Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting SQL Statement to top 5 amounts

Tags:

sql

How do I write a simple SELECT statment which limits the report to only the top 5 of a column value?

like image 237
Trevor Avatar asked Feb 21 '10 15:02

Trevor


1 Answers

You'd have to sort by that column value, maybe in descending order, depending on what you mean by "top 5" ; and fetching only the 5 top lines.

Using MySQL, you'd have something like this, I'd say :

select *
from your_table
where ...
order by your_column desc
limit 5

Using MSSQL server, you don't have limit, but you could use top :

select top 5 *
from your_table
where ...
order by your_column desc
like image 112
Pascal MARTIN Avatar answered Oct 08 '22 16:10

Pascal MARTIN