Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Average Salary by Department

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

Your question is SQL Average Salary 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

Hays wants a department-level compensation summary for its workforce analytics reporting. Write a PostgreSQL query that calculates the average salary of active employees in every department.

Requirements

  1. Return every department, including departments with no active employees.
  2. Calculate the average of salary for active employees only. PostgreSQL AVG should ignore NULL salaries.
  3. Round the average salary to two decimal places and return it as average_salary.
  4. Sort the output by department_id in ascending order.

Schema

departments
ColumnTypeDescription
department_idPKINTEGERUnique department identifier
department_nameVARCHAR(100)Hays department name
employees
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee name
department_idINTEGERAssigned department identifier
salaryNUMERIC(12,2)Annual employee salary
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