MediumSQL challenge #189SUBQUERY

Price Above Category Avg

Find products priced above their category average.

The challenge runs on the SQLabHub practice database (SQLite) and uses the products table. You can solve it in the browser, with nothing to install.

Solve This SQL Challenge

The task

Find products priced above their category average.

A correct answer returns 44 rows with 10 columns: id, name, category, price, cost, stock, sku, status, rating, review_count.

Tables used in this challenge

products 120 rows

id INTEGER (PK) · name TEXT · category TEXT · price REAL · cost REAL · stock INTEGER · sku TEXT · status TEXT · rating REAL · review_count INTEGER

First 3 rows of the products table
idnamecategorypricecoststockskustatusratingreview_count
1LEGO Creator Set 742pcsToys59.9919.8257SKU-000001activeNULL0
2Ergonomic Desk Mat XLOffice Supplies34.9910.85131SKU-000002active4.52
3Magnetic Phone Mount ProAutomotive24.999176SKU-000003active51

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
idnamecategorypricecoststockskustatusratingreview_count
1LEGO Creator Set 742pcsToys59.9919.8257SKU-000001activeNULL0
7RC Off-Road TruckToys69.9922.4453SKU-000007active22
104K Dual Dash CameraAutomotive14946.19411SKU-000010active33
11HEPA Air PurifierHome & Garden17987.71495SKU-000011active3.52
20Whey Protein Powder 2lbFood & Beverage34.9911.55213SKU-000020active2.52

First 5 of 44 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.

What you practise

  • Returning every column of a table with SELECT *
  • Filtering rows with a WHERE condition
  • Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
  • Writing a subquery that refers to the current row of the outer query

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

Correlated subquery

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the products table.
  2. Filter in WHERE: keep the rows where price is greater than the value returned by a subquery.
  3. The subquery is correlated: it refers to the current outer row, so it is evaluated for each row. It works like this:
    1. Read from the products table.
    2. Filter in WHERE: keep the rows where category equals the outer row's category.
    3. Aggregate over all the rows that are left: compute AVG(price). With no GROUP BY, the result is a single row.
  4. Return every column with SELECT *.
Show the model solution
SELECT *
FROM products p
WHERE price > (
  SELECT AVG(price)
  FROM products
  WHERE category = p.category
);

This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.

What to watch for

  • Correlated subquery. A correlated subquery is evaluated per outer row, which gets slow on large tables. A join to a grouped subquery or a window function usually does the same work in one pass.
  • Aggregate functions. Aggregates skip NULLs. AVG divides by the number of non-NULL values and COUNT(column) can be smaller than COUNT(*).
  • WHERE. WHERE is evaluated before the SELECT list, so it cannot use a column alias defined there or the result of an aggregate.
Solve This SQL Challenge

Keep learning