Reputation: 836
I am trying to retrieve the product with the highest price:
SELECT ProductName, Price
FROM [Products]
ORDER BY Price DESC
LIMIT 1
I wanted to know if there is another way of doing this in a more efficient way, with MAX for example.
Upvotes: 0
Views: 55
Reputation: 825
You can use TOP 1
but you always have to consider the possibility of having a tie, so:
SELECT TOP 1 WITH TIES ProductName, Price
FROM [Products]
ORDER BY Price DESC
Upvotes: 0
Reputation: 290
I've always done it with the following
SELECT top 1 Name
FROM tableName
ORDER BY Price DESC
Upvotes: 0
Reputation: 13237
Use MAX and GROUP BY
SELECT ProductName, MAX(Price) [Price]
FROM [Products]
GROUP BY ProductName
ORDER BY MAX(Price) DESC
LIMIT 1;
Upvotes: 1