Module 5 · Advanced queries

Answer questions with subqueries

By the end you will be able to use a query to obtain the value or list of a filter.

About 14 min

0 of 52 lessons

Before you start, you need

Summarise data with COUNT, SUM, AVG, MIN and MAX · Match rows with INNER JOIN · Combine conditions with AND, OR and NOT

Use a query to get a value or a list

You want products that cost more than the average, but you do not know the average yet. You can calculate it in one query and use the answer in another. A query inside another query is called a subquery.

The inner part SELECT AVG(price) FROM products returns a single figure. The outer part compares each price with that figure. The parentheses show where the inner query starts and ends.

The shape of the result decides how to use it. To compare with > you need a single value. If the inner query returns several ids, you can use IN to ask whether a value belongs to that list.

Run the inner part on its own first whenever you want to see what it contributes. In this lesson the inner question does not depend on the outer row; you will learn that connection later with EXISTS.

To use a list, write the query inside the parentheses of IN, for example id IN (SELECT customer_id FROM orders WHERE status = 'shipped'). Each customer is then compared with the ids found in orders.

Minimal syntax
WHERE total > (SELECT AVG(total) FROM orders)

See it in action

Which products cost more than the catalogue's average price?

SQL
SELECT name, price
FROM products
WHERE price > (
  SELECT AVG(price)
  FROM products
);
Example result: 2 rows
nameprice
Pro Laptop999
Monitor 27249
  1. The inner query calculates a single average from all the prices. You can run it on its own.
  2. The outer query compares each price with that threshold and keeps the products above it.
  3. The average does not become a column of products. It is a helper result used to decide which rows get through.
  4. If you are checking membership among several ids, the inner query must supply a list and the outer operator must be IN.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Compare with the average

Not started

Write a query that returns the id and total of the orders whose total is above the average of all orders.

Expected result: 4 rows with the columns id, total, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Get a list

Not started

Show the names of customers whose id is in the list of customer_id values from shipped orders. Use a subquery with IN.

Expected result: 7 rows with the column name, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • A subquery produces a result for another query.
  • A comparison needs a single value.
  • IN accepts a list of values.

Next: Find matches with EXISTS