0
0
SQLquery~5 mins

Correlated subquery execution model in SQL - 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 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?
AOnce for each row of the outer query
BOnly once before the outer query
COnly once after the outer query
DIt does not execute
Which of the following best describes a correlated subquery?
AA subquery that returns multiple columns
BA subquery that runs independently
CA subquery that uses values from the outer query
DA subquery that is always faster
Why might correlated subqueries cause slower query performance?
ABecause they run multiple times, once per outer row
BBecause they do not use indexes
CBecause they return too many columns
DBecause they run only once
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?
AA constant value
BA value from the outer query used in the subquery
CA column from the subquery table only
DAn alias for the subquery
Which statement is true about correlated subqueries?
AThey always return a single value
BThey execute only once and do not depend on the outer query
CThey cannot be used in WHERE clauses
DThey depend on the outer query and execute repeatedly
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.