Brightlane's catalogue team is auditing in the opposite direction now — looking at the categories side rather than the products side.
Write a query to return the category name for every category that has no products currently assigned to it.
Assumptions:
- The
productstable contains every product in the catalogue. - The
categoriestable contains every defined category. - A category is empty if no row in
productshas itsidascategory_id.
Output:
- One row per empty category, with a single column
category_name.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
cat.name AS category_name
FROM
categories cat
FULL OUTER JOIN products p ON cat.id = p.category_id
WHERE
p.id IS NULL The shape
Flipping the audit direction means flipping which side's key the IS NULL filter targets. The outer join produces matched rows, orphan categories, and orphan products. WHERE p.id IS NULL keeps only the orphan-category rows — categories that no product carries as its category_id. For Brightlane that's Clothing and Home & Garden.
Clause by clause
SELECT cat.name AS category_namereturns just the category name. The rest of the join's output is scaffolding for the filter.FROM categories cat FULL OUTER JOIN products p ON cat.id = p.category_idis the reconciliation, written withcategorieson the left. The table position doesn't change whatFULL OUTER JOINdoes — both tables are preserved either way — but reading the query left to right withcategoriesfirst keeps the analyst frame aligned with the question.WHERE p.id IS NULLis the anti-join filter, this time targeting the products side. After the join, any category with no products carriesNULLin everyp.*column, includingp.id. Matched rows have a realp.idand drop out. Orphan products (the kind that have no category) also have a realp.idand drop out — leaving only the empty categories.
Why filter on p.id and not on p.category_id
A learner might reach for WHERE p.category_id IS NULL instead. That filter looks similar but it's testing a different column. Some orphan products have a category_id that's explicitly NULL, and those rows would match the filter even though they're products, not categories. Filtering on p.id is the safe choice because id is the table's primary key — it's NULL only on rows the outer join padded in, never on rows that came from the products table. The rule generalises: the anti-join's IS NULL filter belongs on a column that can only be NULL because the join didn't match, never on a column that can be NULL in the source data.
You practiced isolating the other side's orphans from a FULL OUTER JOIN (or equivalent LEFT JOIN anchored on categories). The recurring rule: which side appears as NULL after the join depends on which side had no match, not on table position — the filter goes on whichever side's key should be missing.