Average leave days per employee by department and year

Quick Overview

Write a SQL query that joins employee and leave tables to find the average leave days per employee for each department and calendar year. It tests joins, grouping by an extracted year, distinct counts in the denominator, and avoiding integer division before rounding.

Average leave days per employee by department and year

Company: Workday

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `leaves` records one day of leave taken by one employee. For each department and calendar year of leave, compute the total number of leave days divided by the number of distinct employees in that department who took leave in that year. Employees who took no leave in a given year do not count toward that year's denominator. Return `department`, `leave_year` and `avg_leave_days_per_employee`, rounded to 2 decimals. ```hint Watch the division Check what PostgreSQL returns when it divides one integer count by another. ``` ### Input Table(s) `employees` | Column | Type | Meaning | |---|---|---| | employee_id | INT | Employee identifier (primary key) | | employee_name | VARCHAR(100) | Employee's name | | department | VARCHAR(100) | The one department the employee belongs to | `leaves` | Column | Type | Meaning | |---|---|---| | leave_id | INT | Leave record identifier (primary key) | | employee_id | INT | Employee who took the leave; matches `employees.employee_id` | | leave_date | DATE | The day of leave | Keys: `employees.employee_id` and `leaves.leave_id` are primary keys. `leaves.employee_id` refers to `employees.employee_id`. ### Output Contract One row per department and year that has at least one leave record: | Column | Type | Meaning | |---|---|---| | department | VARCHAR | Department name | | leave_year | INT | Calendar year of `leave_date` | | avg_leave_days_per_employee | NUMERIC | Leave days in that department and year divided by the number of distinct employees who took them, rounded to 2 decimals | Order by `department` ascending, then `leave_year` ascending. That pair is unique in the output. ### Example `employees` | employee_id | employee_name | department | |---|---|---| | 1 | Ana | Engineering | | 2 | Ben | Engineering | | 3 | Cara | Sales | | 4 | Dev | Sales | | 5 | Eli | Sales | | 6 | Fay | Engineering | `leaves` | leave_id | employee_id | leave_date | |---|---|---| | 1 | 1 | 2023-03-01 | | 2 | 1 | 2023-03-02 | | 3 | 2 | 2023-07-10 | | 4 | 1 | 2024-01-05 | | 5 | 3 | 2023-12-31 | | 6 | 3 | 2024-01-02 | | 7 | 3 | 2024-01-03 | | 8 | 4 | 2024-02-14 | | 9 | 5 | 2024-05-20 | Expected result: | department | leave_year | avg_leave_days_per_employee | |---|---|---| | Engineering | 2023 | 1.50 | | Engineering | 2024 | 1.00 | | Sales | 2023 | 1.00 | | Sales | 2024 | 1.33 | Engineering in 2023 has three leave days (two from Ana, one from Ben) over two employees, so 3 / 2 = 1.50. In 2024 only Ana took leave, so Ben is not in that year's denominator, and Fay never took leave at all. Sales in 2024 has four days over three employees, and 4 / 3 = 1.333 rounds to 1.33. Cara's leave on 2023-12-31 belongs to 2023. ### Constraints and Clarifications - There are 1 to 10,000 employees and 1 to 100,000 leave records. - Every `leave_date` is a valid date. The year is the calendar year of that date. - Each employee belongs to exactly one department, so `department` is never NULL. - Every row of `leaves` counts as one leave day, even when an employee has two rows on the same date. - A leave record whose `employee_id` has no match in `employees` has no department and is left out. - A department and year with no leave records does not appear in the output.

Overview: Write a SQL query that joins employee and leave tables to find the average leave days per employee for each department and calendar year. It tests joins, grouping by an extracted year, distinct counts in the denominator, and avoiding integer division before rounding.

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

Each row of leaves records one day of leave taken by one employee. For each department and calendar year of leave, compute the total number of leave days divided by the number of distinct employees in that department who took leave in that year. Employees who took no leave in a given year do not count toward that year's denominator.

Return department, leave_year and avg_leave_days_per_employee, rounded to 2 decimals.

Input Table(s)

employees

ColumnTypeMeaning
employee_idINTEmployee identifier (primary key)
employee_nameVARCHAR(100)Employee's name
departmentVARCHAR(100)The one department the employee belongs to

leaves

ColumnTypeMeaning
leave_idINTLeave record identifier (primary key)
employee_idINTEmployee who took the leave; matches employees.employee_id
leave_dateDATEThe day of leave

Keys: employees.employee_id and leaves.leave_id are primary keys. leaves.employee_id refers to employees.employee_id.

Output Contract

One row per department and year that has at least one leave record:

ColumnTypeMeaning
departmentVARCHARDepartment name
leave_yearINTCalendar year of leave_date
avg_leave_days_per_employeeNUMERICLeave days in that department and year divided by the number of distinct employees who took them, rounded to 2 decimals

Order by department ascending, then leave_year ascending. That pair is unique in the output.

Example

employees

employee_idemployee_namedepartment
1AnaEngineering
2BenEngineering
3CaraSales
4DevSales
5EliSales
6FayEngineering

leaves

leave_idemployee_idleave_date
112023-03-01
212023-03-02
322023-07-10
412024-01-05
532023-12-31
632024-01-02
732024-01-03
842024-02-14
952024-05-20

Expected result:

departmentleave_yearavg_leave_days_per_employee
Engineering20231.50
Engineering20241.00
Sales20231.00
Sales20241.33

Engineering in 2023 has three leave days (two from Ana, one from Ben) over two employees, so 3 / 2 = 1.50. In 2024 only Ana took leave, so Ben is not in that year's denominator, and Fay never took leave at all. Sales in 2024 has four days over three employees, and 4 / 3 = 1.333 rounds to 1.33. Cara's leave on 2023-12-31 belongs to 2023.

Constraints and Clarifications

  • There are 1 to 10,000 employees and 1 to 100,000 leave records.
  • Every leave_date is a valid date. The year is the calendar year of that date.
  • Each employee belongs to exactly one department, so department is never NULL.
  • Every row of leaves counts as one leave day, even when an employee has two rows on the same date.
  • A leave record whose employee_id has no match in employees has no department and is left out.
  • A department and year with no leave records does not appear in the output.