WeVie
WeVie

Reputation: 598

Selecting only the top 10 of a count in Oracle

I need to create a query in which I only select a top ten of a count in oracle and I cannot seem to get it right. I have read about using rank but so far, I have not figured it out.

select col , count(col) from table_name group by col

Upvotes: 1

Views: 4843

Answers (1)

Mureinik
Mureinik

Reputation: 311163

You could wrap this query with another one, and use the rownum pseudocolumn:

SELECT col, cnt
FROM   (SELECT   col , COUNT(col) AS cnt
        FROM     table_name 
        WHERE    col IS NOT NULL
        GROUP BY col
        ORDER BY 2 DESC)
WHERE  rownum <= 10

Upvotes: 3

Related Questions