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);
CREATE TABLE vat_rates (country TEXT, rate REAL, valid_from TEXT);
INSERT INTO vat_rates VALUES ('Spain', 0.18, '2020-01-01'), ('Spain', 0.21, '2024-01-01'), ('France', 0.20, '2020-01-01');The query as written:
sql
SELECT 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
JOIN vat_rates v ON v.country = c.country
GROUP BY c.country ORDER BY c.country;| country | revenue |
|---|---|
| France | 42 |
| Spain | 86 |
Ana's real revenue is 24 + 51 + 30 = 43, but each line matched both Spanish rates, so 86 is reported. France has one rate and is correct at 42. Bilal (UAE) disappears entirely because there is no rate row for UAE, a second bug caused by the inner join.
A corrected version:
sql
SELECT 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
LEFT JOIN vat_rates v ON v.country = c.country
AND v.valid_from = (SELECT MAX(valid_from) FROM vat_rates v2 WHERE v2.country = c.country)
GROUP BY c.country ORDER BY c.country;| country | revenue |
|---|---|
| France | 42 |
| Spain | 43 |
| UAE | 35 |
Restricting the join to the latest rate gives one match per row, and the LEFT JOIN keeps countries without a rate. A quick check for fan-out is to compare COUNT(*) before and after adding a join. Lookups should have a unique key on the join columns, here (country, valid_from).
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);
CREATE TABLE vat_rates (country TEXT, rate REAL, valid_from TEXT);
INSERT INTO vat_rates VALUES ('Spain', 0.18, '2020-01-01'), ('Spain', 0.21, '2024-01-01'), ('France', 0.20, '2020-01-01');
SELECT 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
JOIN vat_rates v ON v.country = c.country
GROUP BY c.country ORDER BY c.country;
SELECT 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
LEFT JOIN vat_rates v ON v.country = c.country
AND v.valid_from = (SELECT MAX(valid_from) FROM vat_rates v2 WHERE v2.country = c.country)
GROUP BY c.country ORDER BY c.country;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- A non-unique lookup multiplies rows
- Compare row counts before and after a join
- Pick exactly one lookup row per key
- Inner joins to lookups drop unmatched rows
Common mistakes
- Hiding duplicates with DISTINCT inside SUM
- Assuming lookup tables have one row per key
Follow-ups
- How would you apply the rate valid at each order's date?
- How would you enforce one current rate per country?
Tips
- Check the join key's uniqueness