Bird
0
0

Find the issue in this query:

medium📝 Debug Q7 of 15
SQL - Subqueries
Find the issue in this query:

SELECT employee_id, (SELECT MAX(salary) FROM employees WHERE department_id = department_id) AS max_salary FROM employees;
AThe subquery's WHERE clause compares the same column, causing incorrect results
BMissing alias for subquery column
CSubquery returns multiple rows error
DNo issue, query runs correctly
Step-by-Step Solution
Solution:
  1. Step 1: Examine WHERE clause in subquery

    Condition compares department_id to itself, always true, not correlated to outer query.
  2. Step 2: Effect on results

    Subquery returns max salary for all employees, not per department of outer row.
  3. Final Answer:

    The subquery's WHERE clause compares the same column, causing incorrect results -> Option A
  4. Quick Check:

    Incorrect correlation in subquery WHERE clause [OK]
Quick Trick: Correlate subquery columns properly to outer query [OK]
Common Mistakes:
MISTAKES
  • Using same column name without alias causing self-comparison
  • Assuming subquery is correlated when it is not
  • Ignoring effect of incorrect WHERE clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes