Skip to content

Subqueries

Let one query work out the number the other one needs

0 of 3 done · Lesson 8 of 8

Understand

Somebody asks which orders are bigger than average. You know how to get the average and you know how to filter, and the problem is that you need the first answer before you can write the second. Working it out, reading the number and typing it into the next query gives you an answer that is right this morning and wrong by Friday. A subquery is that first query in brackets, run for you every time.

SELECT which_columns_you_want
FROM where_it_lives
WHERE a_column > (SELECT one_value FROM where_it_lives);
SELECTwhich columns you wantFROMwhich tableWHEREwhich rows to keepINmatch against a list

The tables you are working with

8 rows
First rows of the orders table
idcustomerregionamount
3101NorthwindOtago1250
3102Kea LtdWaikato620
3103HalcyonOtago2110
3104BrightsmithAuckland480

4 more rows in the table. Your query sees all 8.