Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Employee Count by Department

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Employee Count by Department. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Synchrony needs a workforce report for departments supporting products such as Synchrony Care. Write a PostgreSQL query that counts active employees by department.

Requirements

  1. Return every department, including departments with no active employees.
  2. Count only employees whose is_active value is TRUE.
  3. Return department_name and active_employee_count, ordered alphabetically by department name.

Place the active-employee condition in the join so that departments with no matching active employees remain in the result. Employees without a valid department should not create an additional department row.

Schema

departments
ColumnTypeDescription
department_idPKINTPrimary key for the department
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTPrimary key for the employee
employee_nameVARCHAR(100)Employee's name
department_idINTAssigned department identifier
is_activeBOOLEANWhether the employee is currently active
Tablesdepartmentsemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results