1

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

PranshuKhandal
  • 661
  • 6
  • 18

3 Answers3

2

Try this for MySQl

SELECT ProductCode FROM OrderDetails
ORDER BY ProductCode Desc 
LIMIT 5
PranshuKhandal
  • 661
  • 6
  • 18
Derviş Kayımbaşıoğlu
  • 26,360
  • 3
  • 47
  • 64
  • my product code is varchar by using this query may i get the exact reult that i want? – Sooraj Thekkepatt Feb 07 '19 at 11:52
  • in this query you seperated OrderDetails by a space, so i joined them – PranshuKhandal Feb 07 '19 at 11:53
  • its not what i wanted.. from my table i want to figure out the top 5 products that orderd by the customers.. its orderdetail table its contain products orderd by diffrent customers only i can figure out by the largest product from the list by its count – Sooraj Thekkepatt Feb 07 '19 at 12:04
  • I gave you solution according to your question. But you are asking something else, However I still couldn't undestand what you need. Please provider sample data and expected output. – Derviş Kayımbaşıoğlu Feb 07 '19 at 12:20
1

solved..by using this query

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

0

it depends on what exactly you need top 5 based on .. it can be sorting by what it can be like numbers first and then the characters one ..
as you said it is varchar then please have a look at this question and limit them with how many u need.

How do I sort a VARCHAR column in SQL server that contains numbers?

Hope this helps :)

Krunal Barot
  • 874
  • 5
  • 17