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.
WHERE total > (SELECT AVG(total) FROM orders)See it in action
Which products cost more than the catalogue's average price?
SELECT name, price
FROM products
WHERE price > (
SELECT AVG(price)
FROM products
);| name | price |
|---|---|
| Pro Laptop | 999 |
| Monitor 27 | 249 |
- The inner query calculates a single average from all the prices. You can run it on its own.
- The outer query compares each price with that threshold and keeps the products above it.
- The average does not become a column of products. It is a helper result used to decide which rows get through.
- 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
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.
First work out the average amount, then compare each order with that single value.
Put it in parentheses.
The outer query selects id and total.
Solution explained
SELECT id, total FROM orders WHERE total > (SELECT AVG(total) FROM orders);The subquery returns a single value, which is what a comparison with each order needs.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Get a list
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.
Does the inner query return a single value or a list of values?
Because it returns a list of customer ids, the outer query uses IN rather than an operator such as >.
Structure: WHERE id IN (SELECT customer_id FROM orders WHERE status = 'shipped').
Solution explained
SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE status = 'shipped');IN compares each customer with a list. Their id appearing several times in the list does not multiply their row.
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