Which SQL statement correctly displays departments having more than 10 employees?

Options

  • A. SELECT department_id, COUNT(*) FROM employees GROUP BY department_id HAVING COUNT(*) > 10;
  • B. SELECT department_id, COUNT(*) FROM employees WHERE COUNT(*) > 10 GROUP BY department_id;
  • C. SELECT department_id FROM employees HAVING COUNT(*) > 10;
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. SELECT department_id, COUNT(*) FROM employees GROUP BY department_id HAVING COUNT(*) > 10;

Detailed Explanation

  • GROUP BY: Creates groups based on department_id.
  • COUNT(*): Counts employees in each department.
  • HAVING: Filters groups whose employee count is greater than 10.
  • Aggregate conditions such as COUNT(*) > 10 belong in HAVING, not WHERE.