SQL · Level 2 Subqueries

SQL Subqueries

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

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);

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.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

products 4 rows
idINTEGERnameTEXTpriceREAL
1Pen2.0
2Lamp30.0
3Desk120.0
4Mug8.0
solution.sql
Test Suites Pending
Output