0

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.

2
  • 2
    Which database please? the features and syntax for solutions differ by platform Commented May 10, 2016 at 7:53
  • LIMIT implies MySQL, but [Products] implies MS SQL Server. Commented May 10, 2016 at 8:15

4 Answers 4

1

Use MAX and GROUP BY

SELECT ProductName, MAX(Price) [Price]
FROM [Products]
GROUP BY ProductName
ORDER BY MAX(Price) DESC
LIMIT 1;
Sign up to request clarification or add additional context in comments.

Comments

0

I've always done it with the following

SELECT top 1 Name
FROM tableName
ORDER BY Price DESC

Comments

0

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

Comments

0
select top 1 * from [Products] order by Price desc 

2 Comments

This will return all products, OP only wants the most expensive one.
sorry for misunderstanding. please check now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.