N049-H2 Tier 4 · Advanced · hard ecommerce · Brightlane

Return every category ID, the total product count, the count of products priced above `$1,000`, and the combined `price` of those luxury products

Part of FILTER Clause on Aggregates in SQL

The problem

Brightlane's luxury product team is auditing the catalog by category.

Write a query to return every category ID, the total product count, the count of products priced above $1,000, and the combined price of those luxury products.

Assumptions:

  • Each category_id with at least one product should appear once.
  • For each category, the total count covers every product. The luxury count covers only products with price strictly greater than $1,000. The luxury total is the combined price across those luxury products.
  • Categories with zero luxury products show a luxury count of 0 and a missing luxury total.

Output:

  • One row per category, with columns category_id, total_products, luxury_count, and luxury_total.
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
  category_id,
  COUNT(*) AS total_products,
  COUNT(*) FILTER (
    WHERE
      price > 1000
  ) AS luxury_count,
  SUM(price) FILTER (
    WHERE
      price > 1000
  ) AS luxury_total
FROM
  products
GROUP BY
  category_id

The shape

The same condition — price > 1000 — restricts a COUNT and a SUM to the luxury subset of each category_id partition. The two aggregates behave differently when no row in the partition matches: COUNT returns 0 because it counts the empty set as zero, and SUM returns NULL because there is nothing to add. That divergence is the audit's tell — every category with zero luxury products shows a luxury count of 0 and a NULL luxury total.

Clause by clause

  • SELECT category_id, COUNT(*) AS total_products, COUNT(*) FILTER (WHERE price > 1000) AS luxury_count, SUM(price) FILTER (WHERE price > 1000) AS luxury_total returns the category, the full product count, the luxury-only count, and the luxury-only price total. The unfiltered COUNT(*) runs across every row; the two filtered aggregates run only across rows where price exceeds $1,000.
  • FROM products reads the product records.
  • GROUP BY category_id partitions the rows per category, including the partition where category_id is NULL — every NULL collapses into one group, so the three uncategorised products land in one row.

Why SUM over zero rows returns NULL and not 0

SUM is defined to add the values it sees. When it sees zero values, there is nothing to add, and the result is NULL. COUNT is defined differently: it counts the rows it sees, and counting zero rows produces 0. The two functions diverge on empty inputs by design. The same divergence holds for AVG, MIN, and MAX — all return NULL on an empty input, because none of them has a defined value for zero observations. Only COUNT returns 0.

The trap

A consumer of this audit who treats the NULL luxury_total as a problem — wrapping the result in arithmetic, or comparing it to a numeric threshold — will get NULL on every category with no luxury products. NULL + anything is NULL; NULL > threshold is NULL, which WHERE treats as not-true. If the next step in the report needs a numeric zero for empty-set categories, the substitution has to be explicit at the point of use; the FILTER itself will not produce one. This is the same NULL-on-empty rule that governs every aggregate function on the platform, and FILTER does not override it.

You practiced COUNT FILTER returning 0 while SUM FILTER returns missing — the same condition produces different empty-set semantics: COUNT over zero matching records is 0; SUM over zero records is missing.

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.