Overview
List the products priced above the average with a subquery, since an aggregate can't go straight into WHERE.
- (SELECT ...) as a value
- Comparing with an aggregate
- Aggregates aren't allowed in WHERE
Short Course
A subquery is a query in brackets used inside another one, for example as a
value to compare with. Trap: an aggregate can't go straight into WHERE:
WHERE price > AVG(price) is an error. Compute it in a subquery.
SELECT name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Task
Return the name and price of every product priced above the average price,
most expensive first. In the database below, the average price is 40.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | nameTEXT | priceREAL |
|---|---|---|
| 1 | Pen | 2.0 |
| 2 | Lamp | 30.0 |
| 3 | Desk | 120.0 |
| 4 | Mug | 8.0 |
Test Suites
Pending
Next
Output