N015-H2 Tier 2 · Core SQL · hard ecommerce · Brightlane

Return the category ID and average list price for every category whose mean price exceeds `$100`

Part of HAVING in SQL

The problem

Brightlane's buying team is evaluating premium-category positioning and needs to identify which product groupings command a higher average price point.

Write a query to return the category ID and average list price for every category whose mean price exceeds $100.

Assumptions:

  • The products table contains every product in Brightlane's catalogue.
  • The average is taken across the price of every product in the category.
  • Categories whose mean price is exactly $100 do not qualify; only those above qualify.

Output:

  • One row per qualifying category_id, with columns category_id and avg_price.
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,
  AVG(price) AS avg_price
FROM
  products
GROUP BY
  category_id
HAVING
  AVG(price) > 100

The shape

GROUP BY category_id builds the per-category set of products; AVG(price) computes each category's mean list price; HAVING AVG(price) > 100 keeps only the categories above $100. The result is six surviving categories, ranging from category_id 6 at 1459 down to category_id 11 at 182.12. Categories whose mean price is at or below $100, or exactly $100, fall out — the comparison is strict.

Clause by clause

  • SELECT category_id, AVG(price) AS avg_price returns the grouping column with the per-category mean. AVG walks the price values for every product in the category and returns the average.
  • FROM products is the source set: every product in Brightlane's catalogue.
  • GROUP BY category_id partitions the catalogue into one group per category. After this clause, each row in the working set represents one category with its average price attached.
  • HAVING AVG(price) > 100 filters those category rows by the mean. The aggregate expression is repeated rather than referenced by the avg_price alias, because aliases are not in scope at HAVING-time.

Why this and not WHERE price > 100

This is the cleanest illustration of the HAVING vs WHERE split. WHERE price > 100 keeps only the individual products priced above $100 and averages those. A category with three products at $90, $110, and $130 would report an average of $120, because the $90 product is excluded before the average runs. HAVING AVG(price) > 100 keeps every product, computes the honest mean across all of them, and only then checks the threshold. That same category correctly averages $110.

Which form is correct depends on the question. "Categories whose mean product price exceeds $100" is the per-category metric, which needs HAVING. "The average price of products that cost more than $100" is the per-row filter, which needs WHERE. The two read similarly in English; in SQL they produce different numbers.

The trap

The trap is that both queries run cleanly and both return a result that looks like "average prices by category." There is no error to flag the wrong choice. The WHERE form silently changes which rows feed into the average, and the answer comes back as a plausible-looking number that is off by however much the excluded rows would have pulled the mean.

The rule: a threshold on an aggregate goes in HAVING. A threshold on the raw inputs to that aggregate goes in WHERE.

You practiced filtering categorical groupings by an aggregate threshold. The shape generalises to any "which segments meet this metric bar" question — the segment is the GROUP BY column, the metric is the aggregate, the bar is the HAVING comparison.

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.