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 →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| store | product | revenue |
|---|---|---|
| Abuja | Power bank | 90 |
| Accra | Smart watch | 150 |
| Lagos | Earbuds | 135 |
Step by step
- 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 - 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 - 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 - 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
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 = 12. 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 = 13. 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 = 14. 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 = 15. 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 <= 2Learn 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 →