Brad Hazelnut
Brad Hazelnut

Reputation: 1621

SQL query to get highest values in a table

I have a table with the following information:

ID          Name        Value       
===         =====       =======
1           apple       5
2           green       10
3           orange      1
4           blue        0
5           fish        3
6           lettuce     2
7           cabbage     4
8           computer    1
9           car     0
10          sport       9
11          racing      15

I want to be able to only pull 3 highest value records in this table. So for example i would want to pull the following in that order.

11          racing      15
2           green       10
10          sport       9

I know i can use ORDER BY to order them by value so it gives me the highest first. But how would i query to only get those records?

Upvotes: 0

Views: 118

Answers (3)

user3741598
user3741598

Reputation: 295

You can ORDER BY multiple columns. If I'm interpreting your request, you could ORDER BY VALUE DESC, NAME. So the result with the 10 would still be first, followed by all those with 0, ordered alphabetically by name.

Upvotes: 1

Hogan
Hogan

Reputation: 70513

In SQL Server

SELECT TOP 3 * FROM tablename order by Value DESC

Upvotes: 2

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You can do as

select * from your_table order by Value desc limit 3

Upvotes: 5

Related Questions