N018-H1 Tier 2 · Core SQL · hard ecommerce · Brightlane

Return the category name for every empty category

Part of LEFT JOIN and RIGHT JOIN in SQL

The problem

Brightlane's catalogue team is auditing category utilisation and needs to identify any categories with no products assigned to them.

Write a query to return the category name for every empty category.

Assumptions:

  • An empty category is one whose id does not appear in products.category_id.
  • The result will contain a small number of rows — most categories have products.

Output:

  • One row per empty category, with a single column category_name.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  cat.name AS category_name
FROM
  categories cat
  LEFT JOIN products p ON cat.id = p.category_id
WHERE
  p.id IS NULL

The shape

categories is the table to enumerate; products is where the existence check happens. A LEFT JOIN from categories to products keeps every category, and WHERE p.id IS NULL keeps only the two whose product columns came back as NULLClothing and Home & Garden. The same anti-join shape, anchored on the dimension side rather than a fact side.

Clause by clause

  • SELECT cat.name AS category_name returns just the category name from the left side — the catalogue team's single-column audit report.
  • FROM categories cat LEFT JOIN products p ON cat.id = p.category_id pairs each category with each of its products. Categories with products produce one row per product with real values in p.*. Empty categories produce a single row with NULL in every p.* column.
  • WHERE p.id IS NULL keeps only the unmatched categories. products.id is the primary key on the right side — it's never NULL on a real row. So a NULL in p.id is unambiguous: the row was synthesised by the outer join to preserve a category that no product points to.

Why this and not categories RIGHT JOIN products

The earlier RIGHT JOIN example in this node preserved categories by putting them on the right; that worked because table position determines preservation. Here the conventional shape — preserve the left table with LEFT JOIN — is cleaner, and most teams standardise on it. Putting categories first in the FROM clause also makes the row set immediately readable: the eye lands on the table being enumerated, exactly where the audit logic starts.

The two queries produce identical results when the table order is flipped and the join keyword is changed. The choice between them is purely a readability convention.

The trap

The trap with dimension-side anti-joins is checking IS NULL on the join key (p.category_id) instead of the primary key (p.id). They happen to work the same way here because every right-side column is NULL for unmatched rows. But category_id is the column being matched on, and if the schema ever allows a real product to have a NULL category_id (an uncategorised product), the filter conflates two different facts: "the category has no products" and "the category has products but at least one is mis-tagged." Checking the primary key (p.id) cleanly isolates the first signal — the row was synthesised by the outer join, nothing more. That's the column to filter on by default.

The broader rule of thumb: anchor the LEFT JOIN on the table being enumerated, then check IS NULL on the other table's primary key. The signal stays clean.

You practiced the anti-join from the dimension side rather than the fact side. The recurring rule of thumb: anchor your LEFT JOIN on whichever table you want to enumerate, then filter for IS NULL on a key from the other side — that key being NULL is the signal that no match exists.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.