Employees earning above their department average
EasyP1Coding
Table employee:
| Column | Type |
|---|---|
| id | int (primary key) |
| name | text |
| salary | int |
| department_id | int |
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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 2 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.