Bird
0
0

What is the problem with this query?

medium📝 Debug Q6 of 15
SQL - Subqueries
What is the problem with this query?

SELECT employee_id, (SELECT department_name FROM departments) AS dept FROM employees;
Assuming departments contains multiple rows.
AThe query will return NULL for all employees
BThe scalar subquery returns multiple rows causing an error
CThe subquery is missing a GROUP BY clause
DThe query will execute successfully without issues
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the subquery

    The subquery (SELECT department_name FROM departments) returns multiple rows because no WHERE clause limits it.
  2. Step 2: Scalar subquery requirement

    A scalar subquery must return exactly one value (one row, one column).
  3. Step 3: Resulting error

    Since multiple rows are returned, the query will raise an error.
  4. Final Answer:

    The scalar subquery returns multiple rows causing an error -> Option B
  5. Quick Check:

    Scalar subqueries must return one value only [OK]
Quick Trick: Scalar subquery must return single value [OK]
Common Mistakes:
MISTAKES
  • Ignoring multiple rows returned by subquery
  • Assuming subquery returns NULL instead of error
  • Not correlating subquery with outer query

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes