Sub topic 1
A Quick Refresher on Subqueries in `WHERE`
A subquery is a query written inside another SQL query. The outer query uses the subquery's result to filter, calculate, or compare rows. This chapter assumes you know SELECT, WHERE, ORDER BY, INNER JOIN, LEFT JOIN, table aliases, aggregate functions, and NULL.
Prerequisites
You should understand how to:
- Select rows from one table.
- Filter rows with
WHERE. - Join related tables.
- Use functions such as
COUNTandAVG. - Give tables short names with aliases.
- Treat
NULLas a missing or unknown value.
Assume these tables:
customers(customer_id, name)orders(order_id, customer_id, order_date, total)
A relationship exists because orders.customer_id identifies the customer who placed each order.
A subquery that returns a value
A subquery in WHERE can calculate a value that the outer query compares with each row. For example, this query finds orders whose total is greater than the average order total:
SELECT order_id, customer_id, total
FROM orders
WHERE total > (
SELECT AVG(total)
FROM orders
);The inner query runs conceptually first:
SELECT AVG(total)
FROM orders;It returns one value. The outer query then keeps orders whose total is greater than that value.
This is an uncorrelated subquery. An uncorrelated subquery does not refer to any column from the outer query. Its result is independent of the row currently being tested by the outer query.
A scalar subquery is a subquery expected to return one value. If the subquery returns multiple rows where one value is required, the database usually reports an error. If it returns no rows, an aggregate such as AVG usually returns NULL.
A subquery that returns a set
A subquery can also return a set of values. The IN operator checks whether a value appears in that set:
SELECT order_id, customer_id, total
FROM orders
WHERE customer_id IN (
SELECT customer_id
FROM customers
WHERE name LIKE 'A%'
);The inner query returns the IDs of customers whose names begin with A. The outer query returns orders belonging to those customers.
You can often rewrite this query with a join:
SELECT o.order_id, o.customer_id, o.total
FROM orders AS o
JOIN customers AS c
ON c.customer_id = o.customer_id
WHERE c.name LIKE 'A%';Both forms express membership in a related result. The subquery can read naturally when the question is “is this value in that set?” The join can be useful when you also need columns from the related table.
The outer row can affect the inner query
A subquery becomes correlated when it refers to a column from the outer query. The inner query then depends on the outer row being considered.
This example compares each order with the average for that order's customer:
SELECT o.order_id, o.customer_id, o.total
FROM orders AS o
WHERE o.total > (
SELECT AVG(o2.total)
FROM orders AS o2
WHERE o2.customer_id = o.customer_id
);The condition o2.customer_id = o.customer_id connects the two query levels. For each outer order o, the inner query calculates an average using only orders for the same customer.
The aliases are important:
oidentifies the current row from the outer query.o2identifies rows scanned by the inner query.o.customer_idsupplies a value from the current outer row.
The next chapter uses this same outer-to-inner link with EXISTS.
Reading a subquery step by step
When you encounter a subquery in WHERE, ask:
- What shape does the inner query return: one value, many values, or a true/false test?
- Does the inner query refer to an outer alias?
- If it is correlated, which outer column supplies the value?
- Can
NULLchange the comparison? - Would a join express the same condition more clearly?
Exercise
What makes the average-total query correlated?
Show answer
The inner query refers to o.customer_id from the outer query, so its average changes for each outer order.