Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Employees Per Department Query

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

Your question is Employees Per Department Query. 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

Auto-Owners Insurance maintains department and employee records for internal workforce reporting. Write a PostgreSQL query that returns the total number of employees assigned to each department.

Requirements

  1. Include every department, including departments with no matching employees.
  2. Count employees per department using an aggregation.
  3. Sort results by employee count descending, then department name ascending for ties.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's name
department_idINTAssigned department identifier
employment_statusVARCHAR(20)Current employment status
Tablesdepartmentsemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results