BBampaa

Lessons › SQL › Go further in SQL: Business questions

SQL lesson

What's the best-selling product in each store?

What's the best-selling product in each store? Two things have to be worked out first: each product's total in each store, then a ranking that starts again for every store. PARTITION BY store makes the ranking start again.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Total, then rank inside each group

WITH totals AS (
  SELECT store, product, SUM(revenue) AS revenue
  FROM orders
  GROUP BY store, product
), ranked AS (
  SELECT store,
         product,
         revenue,
         RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk
  FROM totals
)
SELECT store, product, revenue
FROM ranked
WHERE rnk = 1

Result:

storeproductrevenue
AbujaPower bank90
AccraSmart watch150
LagosEarbuds135

Step by step

  1. Each store's top product.
    WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk = 1
  2. No join.
    WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk = 1
  3. WHERE rnk = 1, at the end.
    WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk = 1
  4. Two CTEs: totals, then ranked.
    WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk = 1

Practice

Try each one in your head first, then open the answer.

1. What's the best-selling product in each store, by revenue? Show store, product and revenue.
WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk = 1
2. What's each store's biggest single order? Show store, order_id and revenue.
WITH ranked AS (SELECT store, order_id, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM orders) SELECT store, order_id, revenue FROM ranked WHERE rnk = 1
3. What's each customer's biggest single order? Show customer_id, order_id and revenue.
WITH ranked AS (SELECT customer_id, order_id, revenue, RANK() OVER (PARTITION BY customer_id ORDER BY revenue DESC) AS rnk FROM orders) SELECT customer_id, order_id, revenue FROM ranked WHERE rnk = 1
4. Which store sells the most of each product, by revenue? Show product, store and revenue.
WITH totals AS (SELECT product, store, SUM(revenue) AS revenue FROM orders GROUP BY product, store), ranked AS (SELECT product, store, revenue, RANK() OVER (PARTITION BY product ORDER BY revenue DESC) AS rnk FROM totals) SELECT product, store, revenue FROM ranked WHERE rnk = 1
5. What are the top 2 products in each store, by revenue? Show store, product and revenue.
WITH totals AS (SELECT store, product, SUM(revenue) AS revenue FROM orders GROUP BY store, product), ranked AS (SELECT store, product, revenue, RANK() OVER (PARTITION BY store ORDER BY revenue DESC) AS rnk FROM totals) SELECT store, product, revenue FROM ranked WHERE rnk <= 2

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed