Bird
0
0

How can you modify this query to find employees who work in departments that have more than 10 employees?

hard📝 Application Q9 of 15
PostgreSQL - Subqueries in PostgreSQL
How can you modify this query to find employees who work in departments that have more than 10 employees?
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments);
AAdd a JOIN instead of using IN with a subquery.
BReplace IN with EXISTS without any subquery changes.
CUse a subquery with GROUP BY and HAVING to filter departments with count > 10 inside the IN clause.
DUse WHERE department_id = ANY (SELECT id FROM departments);
Step-by-Step Solution
Solution:
  1. Step 1: Identify requirement for departments with >10 employees

    We need to count employees per department and filter those with count > 10.
  2. Step 2: Use GROUP BY and HAVING in subquery

    The subquery should group by department ID and use HAVING count(*) > 10.
  3. Step 3: Use this filtered list in IN clause

    The main query filters employees whose department_id is in this filtered list.
  4. Final Answer:

    Use a subquery with GROUP BY and HAVING to filter departments with count > 10 inside the IN clause. -> Option C
  5. Quick Check:

    Use GROUP BY and HAVING in subquery for filtering [OK]
Quick Trick: Use GROUP BY and HAVING in subquery for count filters [OK]
Common Mistakes:
  • Using EXISTS without filtering
  • Replacing IN with ANY without logic change
  • Ignoring grouping and counting in subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes