Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
WITH t AS (
SELECT o.status, i.product, SUM(i.qty) AS total_qty,
RANK() OVER (PARTITION BY o.status ORDER BY SUM(i.qty) DESC) AS rnk
FROM orders o JOIN order_items i ON i.order_id = o.id
GROUP BY o.status, i.product
)
SELECT status, product, total_qty FROM t WHERE rnk = 1 ORDER BY status, product;| status | product | total_qty |
|---|---|---|
| cancelled | Pen | 10 |
| pending | Lamp | 1 |
| shipped | Pen | 7 |
Pen leads in both cancelled and shipped orders. In pending orders only Lamp was sold, so it is the top by default.
Two levels of grouping are involved: the GROUP BY totals quantities per status and product, and the window ranks those totals within each status. The window runs after the GROUP BY, which is why SUM(i.qty) can appear inside its ORDER BY.
Swapping RANK for ROW_NUMBER returns exactly one row per status, which suits a dashboard tile but hides ties. Say which behaviour you chose.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
WITH t AS (
SELECT o.status, i.product, SUM(i.qty) AS total_qty,
RANK() OVER (PARTITION BY o.status ORDER BY SUM(i.qty) DESC) AS rnk
FROM orders o JOIN order_items i ON i.order_id = o.id
GROUP BY o.status, i.product
)
SELECT status, product, total_qty FROM t WHERE rnk = 1 ORDER BY status, product;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Aggregate, then rank within each group
- RANK keeps all ties for first place
- Window functions can order by aggregates
- State the tie behaviour
Common mistakes
- Ranking raw order lines instead of totals
- Using ROW_NUMBER and hiding a tie
Follow-ups
- How would you return the top 2 per status?
- How would you rank by revenue instead?
Tips
- Mention both levels of grouping