By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Correlated subqueries are a type of SQL subquery that depends on the outer query for its execution. Unlike uncorrelated subqueries, which run independently, correlated subqueries execute row-by-row, making them powerful for complex data retrieval but potentially slower. Mastering this concept is crucial for database professionals and exam candidates. Misunderstanding correlated subqueries can lead to inefficient queries, poor performance, and incorrect data retrieval. For instance, a poorly written correlated subquery can significantly slow down a database application, affecting user experience and system performance.
⚠️ Pitfall: Using a correlated subquery where an uncorrelated subquery or JOIN would be more efficient.
Write the Outer Query
sql SELECT employee_id, department_id, salary FROM employees e
⚠️ Pitfall: Writing an overly complex outer query that makes the correlated subquery difficult to understand.
Add the Correlated Subquery
sql WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id)
⚠️ Pitfall: Forgetting to reference the outer query's column correctly, leading to syntax errors.
Optimize the Query
Experts view correlated subqueries as a tool for complex data retrieval but always consider performance. They think in terms of query plans and index usage, balancing the need for row-specific data with the cost of row-by-row execution. Instead of relying solely on correlated subqueries, they explore alternatives like JOINs and window functions to achieve the same results more efficiently.
Exam trap: Questions that can be solved with either a JOIN or a correlated subquery.
The mistake: Forgetting to reference the outer query's column correctly.
Exam trap: Subqueries with missing or incorrect references.
The mistake: Ignoring performance implications.
Exam trap: Questions about query optimization.
The mistake: Writing overly complex outer queries.
Scenario: A company wants to find employees who earn more than the average salary in their department.Question: Write a SQL query to retrieve the employee IDs, department IDs, and salaries of these employees.Solution: 1. Write the outer query to select the required columns.2. Add the correlated subquery to compare each employee's salary with the average salary in their department. sql SELECT employee_id, department_id, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id) Answer: The query will return the employee IDs, department IDs, and salaries of employees who earn more than the average salary in their department.Why it works: The correlated subquery calculates the average salary for each department and compares it with each employee's salary.
sql SELECT employee_id, department_id, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id)
SELECT column FROM table WHERE condition (SELECT column FROM table WHERE condition)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.