Sooraj Thekkepatt
Sooraj Thekkepatt

Reputation: 99

How to find the top 5 values using max() in sql

I want the top 5 products from a table

If i use this

SELECT MAX(ProductCode) AS ProductName 
FROM OrderDetails

I get only one result I want the top 5 result

Upvotes: 0

Views: 3043

Answers (2)

Sooraj Thekkepatt
Sooraj Thekkepatt

Reputation: 99

solved..by using this query

SELECT TOP 5 ProductName, COUNT(ProductName) AS value_occurrence FROM OrderDetails GROUP BY ProductName ORDER BY value_occurrence DESC

Upvotes: 2

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30665

Try this for MySQl

SELECT ProductCode FROM OrderDetails
ORDER BY ProductCode Desc 
LIMIT 5

Upvotes: 2

Related Questions