Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select the top 10 largest numbers from a database column using SQL?

Tags:

sql

I have a database table which contains a column that records page hits for every entry.

I want to select the top 5 most hit pages from the database, but can't seem to find the right method to do so using just SQL. In particular I'm looking for one which doesn't involve selecting every entry and scanning through it afterwards using PHP.

What's the best way to do this via SQL (if there is one)?

Thanks.

like image 321
Jack Roscoe Avatar asked Feb 02 '11 12:02

Jack Roscoe


3 Answers

Try this approach:

SELECT column1, column2, hit_pages,...
FROM YourTable
ORDER BY hit_pages DESC
LIMIT 5
like image 155
eumiro Avatar answered Sep 30 '22 19:09

eumiro


In MySQL> SELECT * FROM table ORDER BY hits DESC limit 5;

In Oralce> SELECT * FROM table ORDER BY hits DESC where rownum <5;

like image 36
diagonalbatman Avatar answered Sep 30 '22 19:09

diagonalbatman


SELECT TOP 10 price(column Name) from products(tablename) order by price desc;

Below is the output

price
263.5 
123.79 
97 
81 
62.5 
55 
53 
49.3 
46 
45.6 
like image 33
Amar Avatar answered Sep 30 '22 21:09

Amar