Sub topic 11
Reporting Zeros: `CROSS JOIN` Plus `LEFT JOIN`
The reporting problem
Assume you know SELECT, WHERE, ORDER BY, INNER JOIN, LEFT JOIN, primary keys, foreign keys, NULL, GROUP BY, and aggregate functions such as SUM. This chapter combines those skills to show every store-product combination, including combinations with no sales.
Suppose a report must show sales for every product in every store. A normal aggregate query only returns combinations that have matching rows in sales. If a store sold zero units of a product, that combination may disappear instead of showing 0.
The solution has two stages:
- Use
CROSS JOINto create every possible store-product combination. - Use
LEFT JOINto attach matching sales while preserving combinations with no sales.
| Stage | Purpose |
|---|---|
| `stores CROSS JOIN products` | Create every store-product pair |
| `LEFT JOIN sales` | Add matching sales without removing pairs |
| `COALESCE(SUM(...), 0)` | Convert missing totals to zero |
Build a complete example
The following statements create a small data set. Downtown sold Widgets, and Uptown sold Gadgets. No other combinations have sales.
CREATE TABLE stores (
id INT PRIMARY KEY,
store_name VARCHAR(50) NOT NULL
);
CREATE TABLE products (
id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL
);
CREATE TABLE sales (
id INT PRIMARY KEY,
store_id INT NOT NULL REFERENCES stores(id),
product_id INT NOT NULL REFERENCES products(id),
quantity INT NOT NULL
);
INSERT INTO stores (id, store_name) VALUES
(1, 'Downtown'),
(2, 'Uptown');
INSERT INTO products (id, product_name) VALUES
(1, 'Widget'),
(2, 'Gadget'),
(3, 'Cable');
INSERT INTO sales (id, store_id, product_id, quantity) VALUES
(1, 1, 1, 5),
(2, 2, 2, 2);There are two stores and three products. The cross join therefore creates 2 × 3 = 6 combinations.
SELECT
st.store_name,
p.product_name,
COALESCE(SUM(s.quantity), 0) AS total_quantity
FROM stores AS st
CROSS JOIN products AS p
LEFT JOIN sales AS s
ON s.store_id = st.id
AND s.product_id = p.id
GROUP BY
st.id,
st.store_name,
p.id,
p.product_name
ORDER BY
st.id,
p.id;The result contains all six combinations:
| store_name | product_name | total_quantity |
|---|---|---|
| Downtown | Widget | 5 |
| Downtown | Gadget | 0 |
| Downtown | Cable | 0 |
| Uptown | Widget | 0 |
| Uptown | Gadget | 2 |
| Uptown | Cable | 0 |
CROSS JOIN makes the six rows before the sales table is considered. The LEFT JOIN keeps each of those rows. When no sale matches, columns from sales contain NULL. SUM over a missing match produces NULL, so COALESCE changes that value to 0.
Why the join condition matters
Put conditions that define a matching sale in the ON clause:
LEFT JOIN sales AS s
ON s.store_id = st.id
AND s.product_id = p.idIf you put a condition on the nullable sales table in WHERE, you can remove the zero rows:
-- This removes combinations without sales.
WHERE s.quantity > 0The WHERE clause runs after the join. For a missing sale, s.quantity is NULL, and NULL > 0 is not true. The row is discarded. If you need to filter sales by date, place that condition in the ON clause instead.
Visualizing the query
Check row growth
A cross join can become large quickly. Ten stores and 1,000 products create 10,000 combinations before the sales join. Check the expected size first:
SELECT
(SELECT COUNT(*) FROM stores)
* (SELECT COUNT(*) FROM products) AS expected_combinations;Use this pattern when the complete set of combinations is genuinely required. If you only need combinations that have sales, join directly to sales instead.
Exercise
Why does the report need CROSS JOIN before LEFT JOIN, rather than starting with sales?
Show answer
CROSS JOIN creates combinations that have no sales rows. Starting with sales cannot preserve a combination that does not exist in sales.