Bird
0
0

Which SQL query correctly groups employees by their department_id and counts the number of employees in each department?

easy📝 Syntax Q3 of 15
SQL - GROUP BY and HAVING
Which SQL query correctly groups employees by their department_id and counts the number of employees in each department?
ASELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
BSELECT department_id, COUNT(employee_id) FROM employees;
CSELECT department_id, COUNT(employee_id) FROM employees WHERE department_id GROUP BY employee_id;
DSELECT COUNT(employee_id) FROM employees GROUP BY department_id;
Step-by-Step Solution
Solution:
  1. Step 1: Grouping by department_id

    The query must group rows by department_id to count employees per department.
  2. Step 2: Counting employees

    Using COUNT(*) counts all rows in each group, which is appropriate here.
  3. Final Answer:

    SELECT department_id, COUNT(*) FROM employees GROUP BY department_id; -> Option A
  4. Quick Check:

    GROUP BY matches COUNT grouping [OK]
Quick Trick: COUNT(*) with GROUP BY groups rows correctly [OK]
Common Mistakes:
MISTAKES
  • Omitting GROUP BY clause
  • Grouping by wrong column
  • Using WHERE instead of GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes