Bird
0
0

Given the tables:

medium📝 query result Q13 of 15
SQL - Subqueries
Given the tables:
employees(emp_id, name, department_id)
departments(department_id, name)
What will this query return?
SELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE name = 'Sales');
ANames of employees who work in the Sales department
BNames of all employees
CNames of departments named Sales
DAn error because subquery returns multiple rows
Step-by-Step Solution
Solution:
  1. Step 1: Understand the subquery

    The subquery selects department_id from departments where name is 'Sales'. This returns IDs of Sales departments.
  2. Step 2: Apply subquery results in main query

    The main query selects employee names where their department_id matches any of those returned by the subquery.
  3. Final Answer:

    Names of employees who work in the Sales department -> Option A
  4. Quick Check:

    Subquery filters employees by Sales department [OK]
Quick Trick: Subquery filters department IDs, main query filters employees [OK]
Common Mistakes:
MISTAKES
  • Thinking subquery returns employee names
  • Confusing department names with employee names
  • Assuming subquery causes error with multiple rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes