Last updated
Identify understaffed departments by finding those with fewer than 2 employees using LEFT JOIN, GROUP BY, and HAVING.
Identify understaffed departments by finding those with fewer than 2 employees using LEFT JOIN, GROUP BY, and HAVING.
Your dataset contains two tables from a company's HR database: 1. A Department table with the ID and name of each department 2. An Employee table with the ID, name, salary, joining date, and department reference for each employee
| Column | Type | Key / Notes |
|---|---|---|
| id | INT | PK |
| name | VARCHAR(50) |
| Column | Type | Key / Notes |
|---|---|---|
| id | INT | PK |
| name | VARCHAR(50) | |
| salary | DECIMAL(10,2) | |
| joining_date | DATE | |
| department_id | INT | FK |
Department — 8 rows
| id | name |
|---|---|
| 1 | HR |
| 2 | Engineering |
| 3 | Marketing |
| 4 | Sales |
| 5 | IT |
| 6 | Finance |
| 7 | Graphic Designer |
| 8 | Data Analyst |
Employee — 20 rows
| id | name | salary | joining_date | department_id |
|---|---|---|---|---|
| 1 | Alice | 70,000 | 2020-01-15 | 1 |
| 2 | Bob | 85,000 | 2019-03-22 | 2 |
| 3 | Charlie | 60,000 | 2021-05-18 | 1 |
| 4 | David | 95,000 | 2018-07-11 | 3 |
| 5 | Eva | 80,000 | 2017-09-09 | 2 |
| 6 | Frank | 75,000 | 2016-11-14 | 3 |
| 7 | Grace | 90,000 | 2015-02-23 | 1 |
| 8 | Henry | 68,000 | 2021-04-30 | 2 |
| 9 | Irene | 72,000 | 2020-06-25 | 1 |
| 10 | Jack | 78,000 | 2019-08-19 | 3 |
| 11 | Karen | 83,000 | 2018-10-07 | 4 |
| 12 | Leo | 95,000 | 2017-12-13 | 1 |
| 13 | Mona | 87,000 | 2016-03-21 | 2 |
| 14 | Nick | 63,000 | 2015-05-29 | 5 |
| 15 | Olivia | 77,000 | 2014-07-15 | 1 |
| 16 | Peter | 82,000 | 2021-01-18 | 2 |
| 17 | Quinn | 91,000 | 2020-03-12 | 6 |
| 18 | Rachel | 88,000 | 2019-09-28 | 1 |
| 19 | Steve | 93,000 | 2018-11-06 | 2 |
| 20 | Tina | 76,000 | 2017-04-16 | 3 |
Find the names of the departments which have less than 2 employees. The final result table should have department_name and count_of_employees columns.
Your output should look like this (partial example):
| department_name | count_of_employees |
|---|---|
| Data Analyst | 0 |
| Graphic Designer | 0 |
| … | … |