N052-M4 Tier 4 · Advanced · medium ecommerce · Brightlane

Return every category's name, depth level, and number of products assigned directly to that category

Part of Recursive CTEs in SQL

The problem

Brightlane's product catalog dashboard reports the category hierarchy alongside direct product counts at each level.

Write a query to return every category's name, depth level, and number of products assigned directly to that category.

Assumptions:

  • Root categories have a missing parent_id.
  • Every category appears in the result exactly once. Root categories are at depth 1; each subcategory's depth is one greater than its parent's depth.
  • The product count for a category is the number of products whose category_id equals that category's id. Categories with no products show a count of 0.

Output:

  • One row per category, with columns name, depth, and product_count.
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
WITH RECURSIVE
  cat_tree AS (
    SELECT
      id,
      name,
      parent_id,
      1 AS depth
    FROM
      categories
    WHERE
      parent_id IS NULL
    UNION ALL
    SELECT
      c.id,
      c.name,
      c.parent_id,
      ct.depth + 1
    FROM
      categories c
      JOIN cat_tree ct ON c.parent_id = ct.id
  )
SELECT
  ct.name,
  ct.depth,
  COUNT(p.id) AS product_count
FROM
  cat_tree ct
  LEFT JOIN products p ON p.category_id = ct.id
GROUP BY
  ct.name,
  ct.depth

The shape

Two layers. The recursive CTE produces one row per category with its depth attached. The main query then LEFT JOINs that hierarchy to products on category_id and counts matches per category. The LEFT JOIN is what keeps categories with zero products in the result instead of dropping them.

Clause by clause

  • The recursive CTE walks the category tree:
WITH RECURSIVE cat_tree AS (
    SELECT id, name, parent_id, 1 AS depth
    FROM categories
    WHERE parent_id IS NULL
    UNION ALL
    SELECT c.id, c.name, c.parent_id, ct.depth + 1
    FROM categories c
    JOIN cat_tree ct ON c.parent_id = ct.id
)

The anchor seeds the four root categories at depth 1. The recursive member matches each subcategory's parent_id to a row already in cat_tree and stamps it with ct.depth + 1. After two passes, cat_tree contains every category with the correct depth.

  • The main query attaches per-category product counts:
SELECT ct.name, ct.depth, COUNT(p.id) AS product_count
FROM cat_tree ct
LEFT JOIN products p ON p.category_id = ct.id
GROUP BY ct.name, ct.depth

The LEFT JOIN keeps every category row even when no product matches; unmatched rows carry a NULL in p.id. GROUP BY ct.name, ct.depth collapses the joined rows per category, and COUNT(p.id) counts non-NULL product ids — which is zero for categories with no products. Clothing and Home & Garden both show product_count = 0; Electronics shows 12; Phones, Furniture, and Outdoor show 6, 8, and 8.

Why LEFT JOIN and COUNT(p.id), not COUNT(*)

The pairing of LEFT JOIN and COUNT(p.id) is the standard preservation pattern. LEFT JOIN keeps unmatched category rows; COUNT(p.id) correctly returns 0 for those rows because it counts non-NULL product ids. COUNT(*) would return 1 for unmatched rows, counting the placeholder NULL-padded row as if it were a product. The category rows would look like every empty category has exactly one product, which is wrong.

The trap

The LEFT JOIN direction matters. cat_tree LEFT JOIN products keeps every category and lets product matches attach; reversing it to products LEFT JOIN cat_tree would keep every product and lose the empty categories. The category side has to be the preserved side, since the question is "how many products does each category have," not "how many categories does each product belong to."

You practiced combining WITH RECURSIVE with a downstream LEFT JOIN — the recursion produces the hierarchy; the outer left-join attaches per-category counts and preserves categories with zero matches.

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.