| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> comp.databases.theory -> Re: Can I do this using SQL?
dhruv wrote:
> Hello,
> Maybe I'm missing something, but I can't figure out a way to write an
> SQL query for the floowing requirements:
>
> I have a table which holds accounts of customers. Now, the schema is as
> follows:
>
> CUST_NAME, BRANCH_NAME, BALANCE, CUST_ID
> [ok, yes, it's not _actually_ like this, but this is the result of a
> join on some obviously normalized tables].
>
> Now, I want to get a list of the TOP 10 Branches wrt. the number
> customers they have. How would I do that?
>
> First I thought grouping or sorting would help, but sort on frequencey?
> How do I do that in SQL?
This will give you all
SELECT branch_name, COUNT(cust_id) AS cnt
FROM ...
GROUP BY branch_name
ORDER BY 2 DESC
If you omit the ORDER BY clause, you have a simple table on which to operate
and with some subqueries you can easily filter the first 10 rows. (Unless
your specific DBMS has a "TOP n", "FETCH FIRST n ROWS" or "LIMIT"
capability.)
-- Knut StolzeReceived on Thu Feb 09 2006 - 06:12:07 CST
![]() |
![]() |