Department-wise Employee Count for Departments With More Than 5 Employees

Quick Overview

Write a SQL query that counts the employees in each department and returns only the departments with more than five employees, along with their names and counts. It tests joins, grouping, filtering on an aggregate, and how COUNT treats the NULL rows that an outer join produces.

Department-wise Employee Count for Departments With More Than 5 Employees

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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`. ```hint Where the filter goes The threshold applies to a count, so the condition can only be checked after the rows are grouped by department. ``` ### 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.

Overview: Write a SQL query that counts the employees in each department and returns only the departments with more than five employees, along with their names and counts. It tests joins, grouping, filtering on an aggregate, and how COUNT treats the NULL rows that an outer join produces.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
department_idINTDepartment identifier (primary key)
department_nameVARCHAR(100)Department name, not null and unique

Table: employees

ColumnTypeMeaning
employee_idINTEmployee identifier (primary key)
employee_nameVARCHAR(100)Employee name, not null
department_idINTThe 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_iddepartment_name
1Engineering
2Sales
3Legal

employees

employee_idemployee_namedepartment_id
101Ana1
102Ben1
103Chen1
104Dev1
105Eli1
106Fay1
107Gus2
108Hana2
109Ivan2
110Jo2
111Kai2

Expected output:

department_iddepartment_nameemployee_count
1Engineering6

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.