0
0
MySQLquery~5 mins

Correlated subqueries in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly constants
BValues from the outer query
COnly the inner query's tables
DNo dependencies
Which of these is true about correlated subqueries?
AThey run once per row of the outer query
BThey always return multiple rows
CThey cannot use outer query columns
DThey run once for the entire query
In MySQL, where can a correlated subquery appear?
AOnly in JOIN conditions
BOnly in FROM clause
COnly in WHERE clause
DIn WHERE, SELECT, or HAVING clauses
What is a common use case for correlated subqueries?
ACreating indexes
BJoining two unrelated tables
CComparing each row to an aggregate of related rows
DUpdating multiple tables at once
Why might you avoid correlated subqueries on large tables?
AThey can be slow due to repeated execution
BThey use too much disk space
CThey do not work with indexes
DThey cause syntax errors
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.