Write a query that lists each department with its number of employees, keeping only departments that have more than 5 employees. Return department_id, department_name, and employee_count.
Input Table(s)
Table: departments
| Column | Type | Meaning |
|---|
| department_id | INT | Department identifier (primary key) |
| department_name | VARCHAR(100) | Department name, not null and unique |
Table: employees
| Column | Type | Meaning |
|---|
| employee_id | INT | Employee identifier (primary key) |
| employee_name | VARCHAR(100) | Employee name, not null |
| department_id | INT | The employee's department (foreign key to departments.department_id) |
Keys: departments.department_id and employees.employee_id are primary keys. employees.department_id references departments.department_id.
Output Contract
-
Columns, in this order:
department_id
,
department_name
,
employee_count
(an integer count of the department's employees).
-
One row per department with more than 5 employees. A department with exactly 5 employees is excluded.
-
Sort by
department_id
ascending. It is the primary key, so the order is unique.
-
No rounding is involved.
Example
departments
| department_id | department_name |
|---|
| 1 | Engineering |
| 2 | Sales |
| 3 | Legal |
employees
| employee_id | employee_name | department_id |
|---|
| 101 | Ana | 1 |
| 102 | Ben | 1 |
| 103 | Chen | 1 |
| 104 | Dev | 1 |
| 105 | Eli | 1 |
| 106 | Fay | 1 |
| 107 | Gus | 2 |
| 108 | Hana | 2 |
| 109 | Ivan | 2 |
| 110 | Jo | 2 |
| 111 | Kai | 2 |
Expected output:
| department_id | department_name | employee_count |
|---|
| 1 | Engineering | 6 |
Engineering has 6 employees, which is more than 5. Sales has exactly 5 and is left out. Legal has no employees and is left out.
Constraints and Clarifications
-
departments
has at most 100 rows and
employees
at most 10,000 rows.
-
Every
department_id
in
employees
references a valid row in
departments
.
-
department_name
is unique and non-null.
-
employee_id
is unique, so each employee is counted once.
-
A department with no employees has a count of 0 and never appears in the output.