Bird
0
0

Given tables employees(id, name) and projects(id, employee_id), what does this query return?

medium📝 query result Q4 of 15
PostgreSQL - Subqueries in PostgreSQL
Given tables employees(id, name) and projects(id, employee_id), what does this query return?
SELECT name FROM employees WHERE EXISTS (SELECT 1 FROM projects WHERE projects.employee_id = employees.id);
AAn error because of missing JOIN
BNames of employees who have at least one project
CAll employee names regardless of projects
DNames of employees who have no projects
Step-by-Step Solution
Solution:
  1. Step 1: Understand the EXISTS subquery

    The subquery checks if any project exists for each employee by matching employee_id.
  2. Step 2: Interpret the WHERE clause

    WHERE EXISTS filters employees who have at least one matching project row.
  3. Final Answer:

    Names of employees who have at least one project -> Option B
  4. Quick Check:

    EXISTS filters employees with projects = D [OK]
Quick Trick: EXISTS filters rows where subquery finds matches [OK]
Common Mistakes:
  • Thinking it returns employees without projects
  • Assuming it returns all employees
  • Expecting a syntax error due to no JOIN

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes