Recall & Review
beginner
What is a correlated subquery in SQL?
A correlated subquery is a subquery that depends on values from the outer query. It runs once for each row processed by the outer query.
Click to reveal answer
intermediate
How does the execution model of 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, using values from that row.
Click to reveal answer
intermediate
Why can correlated subqueries be slower than non-correlated subqueries?
Because correlated subqueries execute once per outer row, they may run many times, causing slower performance compared to subqueries that run only once.
Click to reveal answer
beginner
Give an example of a correlated subquery in SQL.
Example: <br>SELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department = e1.department); <br>This subquery depends on e1.department from the outer query.
Click to reveal answer
beginner
What is the role of the outer query in a correlated subquery execution?
The outer query provides values to the subquery for each row, controlling how many times the subquery runs and what data it uses.
Click to reveal answer
How often does a correlated subquery execute in relation to the outer query?
✗ Incorrect
A correlated subquery runs once for each row processed by the outer query because it depends on values from that row.
Which of the following best describes a correlated subquery?
✗ Incorrect
A correlated subquery depends on the outer query's current row values to execute.
Why might correlated subqueries cause slower query performance?
✗ Incorrect
Correlated subqueries execute repeatedly for each outer row, increasing total execution time.
In the query: SELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department = e1.department); what is e1.department?
✗ Incorrect
e1.department is from the outer query and is used inside the subquery, making it correlated.
Which statement is true about correlated subqueries?
✗ Incorrect
Correlated subqueries depend on the outer query and execute once per outer row.
Explain how a correlated subquery executes in relation to its outer query.
Think about how the subquery needs data from each row of the outer query.
You got /4 concepts.
Describe a real-life example where a correlated subquery might be useful.
Imagine checking if a person's salary is above their department's average.
You got /4 concepts.