RianAcademy
Search
⌘
K
Premium

Correlated subqueries

All previous subqueries we've discussed were uncorrelated (independent). They could be executed autonomously from the main query, and we could see what they returned before their result was used in the main query. Correlated subqueries, on the other hand, refer to one or more columns in the main query.

Example of a correlated subquery

For example, the following correlated subquery finds out who spent how much:

MySQL 8.4
SELECT FamilyMembers.member_name, (
    SELECT SUM(Payments.unit_price * Payments.amount)
    FROM Payments
    WHERE Payments.family_member = FamilyMembers.member_id
) AS total_spent
FROM FamilyMembers;
member_nametotal_spent
Headley Quincey2504
Flavia Quincey74194
Andie Quincey3600
Lela Quincey650
Annie Quincey1060
Ernest Forrest<NULL>
Constance Forrest<NULL>
Wednesday Addams<NULL>

In this case, the correlated subquery refers to the member_id column in the main query.

A correlated subquery differs from an uncorrelated subquery in that it is not executed once before the query in which it is nested, but for each row that can be included in the final result.

In our case, the main query to the FamilyMembers table returns 8 records, and a correlated subquery is executed for each of them.

Performance impact

It should be noted that the use of correlated subqueries can cause performance problems, especially if the containing query returns a lot of rows, since the correlated subquery will be executed separately for each row of the containing query.

Tasks for self-testing
0 / 2
1. Getting the most expensive purchased item

Using a correlated subquery, output the names of all family members (member_name) and the cost of their most expensive purchased item.
To display the cost of the most expensive purchased product, use the alias good_price. If there is no such product, output NULL.
FamilyMembers
member_id
INT
status
VARCHAR
member_name
VARCHAR
birthday
DATETIME
Goods
good_id
INT
good_name
VARCHAR
type
INT
GoodTypes
good_type_id
INT
good_type_name
VARCHAR
Payments
payment_id
INT
family_member
INT
good
INT
amount
INT
unit_price
INT
date
DATETIME
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
FamilyMembers
Goods
GoodTypes
Payments

Run a query to see the result, or open a table above