Bird
0
0

Given the tables:

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

    The subquery selects department IDs where the department name is 'Sales'.
  2. Step 2: Filter employees by matching department IDs

    The main query returns employee names whose department_id matches those IDs.
  3. Final Answer:

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

    IN filters employees by Sales department IDs [OK]
Quick Trick: Subquery filters departments, main query filters employees [OK]
Common Mistakes:
  • Thinking it returns all employees
  • Confusing NOT IN with IN
  • Assuming subquery causes error with multiple rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes