Ivy Professional School
Rating
Learning/SQL Data Drill #2

Skeleton Crew

Last updated

Identify understaffed departments by finding those with fewer than 2 employees using LEFT JOIN, GROUP BY, and HAVING.

SQLBeginnerLEFT JOINGROUP BYHAVINGCOUNT

Your Objective

Identify understaffed departments by finding those with fewer than 2 employees using LEFT JOIN, GROUP BY, and HAVING.

The Setup

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

Table Schema

Department
ColumnTypeKey / Notes
idINTPK
nameVARCHAR(50)
Employee
ColumnTypeKey / Notes
idINTPK
nameVARCHAR(50)
salaryDECIMAL(10,2)
joining_dateDATE
department_idINTFK

Sample Data

Department — 8 rows

idname
1HR
2Engineering
3Marketing
4Sales
5IT
6Finance
7Graphic Designer
8Data Analyst

Employee — 20 rows

idnamesalaryjoining_datedepartment_id
1Alice70,0002020-01-151
2Bob85,0002019-03-222
3Charlie60,0002021-05-181
4David95,0002018-07-113
5Eva80,0002017-09-092
6Frank75,0002016-11-143
7Grace90,0002015-02-231
8Henry68,0002021-04-302
9Irene72,0002020-06-251
10Jack78,0002019-08-193
11Karen83,0002018-10-074
12Leo95,0002017-12-131
13Mona87,0002016-03-212
14Nick63,0002015-05-295
15Olivia77,0002014-07-151
16Peter82,0002021-01-182
17Quinn91,0002020-03-126
18Rachel88,0002019-09-281
19Steve93,0002018-11-062
20Tina76,0002017-04-163

Your Task

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_namecount_of_employees
Data Analyst0
Graphic Designer0
……

Hint

  • →Think carefully about which type of JOIN to use. Some departments may have zero employees — will an INNER JOIN show them?
  • →Remember: WHERE filters rows before grouping, HAVING filters groups after aggregation. Which one works with COUNT?
Explore Enterprise Solutions