← Back to SQL, page 5

sql-0091 · Easy fresher

Write a query for the three best-selling products by revenue, excluding cancelled orders.

Join items to orders, filter out cancelled orders in WHERE, group by product, sum qty * unit_price, order by that sum descending and LIMIT 3. Add a tie-breaker, and consider whether ties at third place should all be shown.

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
SELECT i.product, SUM(i.qty * i.unit_price) AS revenue
FROM order_items i
JOIN orders o ON o.id = i.order_id
WHERE o.status <> 'cancelled'
GROUP BY i.product
ORDER BY revenue DESC, i.product
LIMIT 3;
productrevenue
Bag60
Lamp25
Notebook20

Bag leads with two sales of 30. The cancelled order 103 had 10 pens, so the status filter matters for Pen. Without the filter and without the LIMIT, the full ranking is:

sql
SELECT i.product, SUM(i.qty * i.unit_price) AS revenue
FROM order_items i
GROUP BY i.product
ORDER BY revenue DESC, i.product;
productrevenue
Bag60
Lamp25
Notebook20
Pen17

Pen shows 17 instead of the real 7. On this small data the top three happen not to change, but a single large cancelled order can easily push a product into the list, so the business rule has to be written into the query.

LIMIT 3 cuts at exactly three rows. If two products tied for third place, one would be dropped without any sign. When ties matter, rank the products with a window function such as DENSE_RANK() and keep ranks up to 3, or use FETCH FIRST 3 ROWS WITH TIES on PostgreSQL.

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);

SELECT i.product, SUM(i.qty * i.unit_price) AS revenue
FROM order_items i
JOIN orders o ON o.id = i.order_id
WHERE o.status <> 'cancelled'
GROUP BY i.product
ORDER BY revenue DESC, i.product
LIMIT 3;

SELECT i.product, SUM(i.qty * i.unit_price) AS revenue
FROM order_items i
GROUP BY i.product
ORDER BY revenue DESC, i.product;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Filter business exclusions before aggregating
  • Group, sum revenue per product, order descending, LIMIT
  • Add a tie-breaker so the order is repeatable
  • LIMIT drops tied rows; use ranking or WITH TIES if ties matter

Common mistakes

  • Including cancelled or refunded orders by accident
  • Ranking by quantity when the question asks for revenue

Follow-ups

  • How would you get the top product per country?
  • How would you show ties at third place?

Tips

  • Show how one business rule changes the answer