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);
INSERT INTO customers VALUES (5, 'Esa', 'Spain');
INSERT INTO orders VALUES (107, 5, '2026-03-20', 'shipped');
INSERT INTO order_items VALUES (107, 'Bag', 1, 30), (107, 'Notebook', 3, 4), (107, 'Pen', 1, 1);sql
WITH revenue AS (
SELECT c.id, c.name, c.country, SUM(oi.qty * oi.unit_price) AS revenue
FROM customers c JOIN orders o ON o.customer_id = c.id JOIN order_items oi ON oi.order_id = o.id
WHERE o.status = 'shipped' GROUP BY c.id
)
SELECT country, name, revenue FROM (
SELECT *, RANK() OVER (PARTITION BY country ORDER BY revenue DESC) AS rnk FROM revenue
) WHERE rnk = 1 ORDER BY country, name;| country | name | revenue |
|---|---|---|
| France | Chloe | 42 |
| Spain | Ana | 43 |
| Spain | Esa | 43 |
Ana and the added customer Esa both have 43 in shipped revenue in Spain, so both rank first and both are returned. Chloe leads France with 42. Bilal (UAE) has no shipped orders and is absent from the revenue CTE altogether; a LEFT JOIN with COALESCE would include such customers with zero. Changing RANK to ROW_NUMBER would return only one of the two Spanish customers, and which one would not be defined without a tiebreaker.
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);
INSERT INTO customers VALUES (5, 'Esa', 'Spain');
INSERT INTO orders VALUES (107, 5, '2026-03-20', 'shipped');
INSERT INTO order_items VALUES (107, 'Bag', 1, 30), (107, 'Notebook', 3, 4), (107, 'Pen', 1, 1);
WITH revenue AS (
SELECT c.id, c.name, c.country, SUM(oi.qty * oi.unit_price) AS revenue
FROM customers c JOIN orders o ON o.customer_id = c.id JOIN order_items oi ON oi.order_id = o.id
WHERE o.status = 'shipped' GROUP BY c.id
)
SELECT country, name, revenue FROM (
SELECT *, RANK() OVER (PARTITION BY country ORDER BY revenue DESC) AS rnk FROM revenue
) WHERE rnk = 1 ORDER BY country, name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Aggregate, then rank within partitions
- RANK keeps ties at the top
- ROW_NUMBER picks one arbitrarily
- Customers without qualifying rows drop out
Common mistakes
- Using ROW_NUMBER when ties should be kept
- Ranking before aggregating
Follow-ups
- How would you keep the top two per country?
- How would you include customers with no shipped orders?
Tips
- Explain RANK versus ROW_NUMBER