Recall & Review
beginner
What is a correlated subquery in SQL?
A correlated subquery is a subquery that uses values from the outer query. It runs once for each row processed by the outer query.
Click to reveal answer
beginner
How does a correlated subquery differ from a regular subquery?
A regular subquery runs once and returns a result set. A correlated subquery runs repeatedly, once for each row of the outer query, because it depends on outer query values.
Click to reveal answer
intermediate
Example: What does this query do?<br>
SELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department = e1.department);
This query finds employees whose salary is greater than the average salary of their own department. The subquery uses e1.department from the outer query, so it is correlated.
Click to reveal answer
intermediate
Why can correlated subqueries be slower than regular subqueries?
Because the correlated subquery runs once for each row of the outer query, it can be slower especially on large tables. It repeats work multiple times.
Click to reveal answer
intermediate
Can a correlated subquery appear in the SELECT clause?
Yes, a correlated subquery can appear in the SELECT clause to calculate a value for each row, like a computed column based on related data.
Click to reveal answer
What does a correlated subquery depend on?
✗ Incorrect
A correlated subquery depends on values from the outer query to run for each row.
Which of these is true about correlated subqueries?
✗ Incorrect
Correlated subqueries run once for each row of the outer query because they depend on outer values.
In MySQL, where can a correlated subquery appear?
✗ Incorrect
Correlated subqueries can appear in WHERE, SELECT, or HAVING clauses to filter or compute values.
What is a common use case for correlated subqueries?
✗ Incorrect
Correlated subqueries often compare each row to aggregates like averages or counts from related rows.
Why might you avoid correlated subqueries on large tables?
✗ Incorrect
Correlated subqueries can be slow because they run once per outer row, repeating work many times.
Explain what a correlated subquery is and how it works in a SQL query.
Think about how the inner query uses data from the outer query.
You got /3 concepts.
Describe a scenario where using a correlated subquery is helpful and what to watch out for.
Consider real-life examples like comparing a student's score to class average.
You got /3 concepts.