Find every store-product listing gap
A category manager wants a full coverage matrix: every store should ideally list every active product. The team needs the gaps.
Build the complete store × product grid and return store_code, product_code and product_name for every combination that is missing from store_listings. Only include products where is_active is true.
Sample data
Table: stores
4 row(s)
| store_codeTEXT | store_cityTEXT |
|---|---|
| ST-01 | Pune |
| ST-02 | Hyderabad |
| ST-03 | Kolkata |
| ST-04 | Jaipur |
Table: products
5 row(s)
| product_codeTEXT | product_nameTEXT | is_activeBOOLEAN |
|---|---|---|
| PR-A | Cold Brew Concentrate | true |
| PR-B | Millet Cereal Pack | true |
| PR-C | Bamboo Toothbrush | true |
| PR-D | Discontinued Gift Box | false |
| PR-E | Steel Water Bottle | true |
Table: store_listings
14 row(s) · showing first 8
| store_codeTEXT | product_codeTEXT | shelf_unitsINT |
|---|---|---|
| ST-01 | PR-A | 12 |
| ST-01 | PR-B | 8 |
| ST-01 | PR-C | 5 |
| ST-01 | PR-E | 9 |
| ST-02 | PR-A | 15 |
| ST-02 | PR-C | 4 |
| ST-03 | PR-B | 11 |
| ST-03 | PR-D | 2 |
stores
- store_codeTEXT
- store_cityTEXT
products
- product_codeTEXT
- product_nameTEXT
- is_activeBOOLEAN
store_listings
- store_codeTEXT
- product_codeTEXT
- shelf_unitsINT
- Step 1: The universe of what *should* exist is every store paired with every product — that is exactly a `CROSS JOIN` between `stores` and `products`.
- Step 2: `LEFT JOIN store_listings` on both keys to check which of those pairs actually exist today.
- Step 3: Pairs with no listing produce `NULL`s on the listing side, so `l.product_code IS NULL` isolates the gaps.
- Step 4: Restrict to sellable items with `p.is_active` so the discontinued gift box is not reported as a gap.
- Step 5: Return the store, the product code and the product name for each missing combination.
SELECT s.store_code, p.product_code, p.product_name
FROM stores AS s
CROSS JOIN products AS p
LEFT JOIN store_listings AS l
ON l.store_code = s.store_code
AND l.product_code = p.product_code
WHERE p.is_active
AND l.product_code IS NULL;Approach 2 — EXCEPT against the existing grid
Set operators express the same idea: full grid minus what already exists.
SELECT s.store_code, p.product_code, p.product_name
FROM stores AS s
CROSS JOIN products AS p
WHERE p.is_active
AND (s.store_code, p.product_code) NOT IN (
SELECT store_code, product_code FROM store_listings
);Learn the concept first
Stuck on Joins — Cross Join? These free SQL lessons teach it step by step.
More Joins — Cross Join questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program