Count employees per department, including empty departments
EasyP2Coding
Tables:
department(id int primary key, name text)
employee(id int primary key, department_id int) (department_id can be NULL)
Return every department with its number of employees, including departments with none (count 0).
Output columns: name, employee_count. Two departments can share a name, so they must stay separate rows.
Schema
CREATE TABLE department ( id int PRIMARY KEY, name text NOT NULL ); CREATE TABLE employee ( id int PRIMARY KEY, department_id int );
Example 1
- Input
- department: (1, Eng), (2, HR); employee: (1, 1), (2, 1)
- Output
- Eng: 2, HR: 0
Example 2
- Input
- department: (1, Ops), (2, Ops); employee: (1, 1)
- Output
- Ops: 1, Ops: 0
Constraints
- PostgreSQL dialect.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 3 visible tests, Submit all 5. ⌘Enter runs, ⌘⇧Enter submits.