-1

I'd like to know a solution to listing the top 3 or so most ordered products in a database, as a basic example.

I currently know how to list in Ascending/Descending order but I do not know how to display the most popular orders.

Any help would be greatly appreciated.

user3202071
  • 1
  • 1
  • 1
  • 2

2 Answers2

1

Well, without seeing your database model, this is pure conjecture. I'll give it a shot any way.

Let's say you have two tables: one for products and another for orders.

Products is:

Product_ID
Product_Name
Product_Price

Orders is:

Order_ID
Product_ID
Order_Date

You can do something like:

select Product_ID, count(Product_ID) as OrderCount
from Orders
group by Product_ID
order by OrderCount desc

This will give you the order count for each product. You can then filter down the list to however many you want to actually display.

Justin Skiles
  • 9,373
  • 6
  • 50
  • 61
0

you can do this through your "orders" table by grouping by product_key and selecting top 3 if you want a more detailed/useful answer post ur DB structure but this is an example of what I mean

select top 3 , count(*) from orders group_by product_key order by count(*) DESC
Hussein Negm
  • 551
  • 5
  • 17