Skip to content
Onsite

Employees earning above their department average

EasyP1Coding

Table employee:

ColumnType
idint (primary key)
nametext
salaryint
department_idint

Write a query that returns the employees whose salary is strictly above the average salary of their own department. Return the columns id and name.

Schema

CREATE TABLE employee (
  id int PRIMARY KEY,
  name text NOT NULL,
  salary int NOT NULL,
  department_id int NOT NULL
);

Example 1

Input
employee: (1, 'A', 100, 1), (2, 'B', 200, 1), (3, 'C', 50, 2)
Output
id: 2, name: B
Why
Department 1 averages 150. C is alone in department 2, so equals its own average and is not above it.

Constraints

  • PostgreSQL dialect.
  • The average includes the employee being compared.
  • Return rows in any order.

Hints

PostgreSQL
Run checks the 2 visible tests, Submit all 6.