Skip to content
UniCircle
MediumSQLGROUP BY and HAVING

Departments paid above the company average

Question SQL-GRP-002

Table:

SQL
CREATE TABLE employees (id int PRIMARY KEY, name text NOT NULL, dept text NOT NULL, salary int NOT NULL);

List the departments whose average salary is strictly greater than the average salary of the whole company. Return one column dept, sorted alphabetically.

Input and output

Output
One department per row.

Examples

Input 1

CREATE TABLE employees (id int PRIMARY KEY, name text NOT NULL, dept text NOT NULL, salary int NOT NULL);
INSERT INTO employees VALUES (1,'A','Eng',100),(2,'B','Eng',80),(3,'C','Sales',50),(4,'D','Sales',60),(5,'E','HR',40);

Output

Eng

Company average 66.

Input 2

CREATE TABLE employees (id int PRIMARY KEY, name text NOT NULL, dept text NOT NULL, salary int NOT NULL);
INSERT INTO employees VALUES (1,'A','Eng',70),(2,'B','Ops',90);

Output

Ops

Average 80.

Show hints
  • Filtering on an aggregate needs HAVING, not WHERE.
Show the explanation

Group by department and keep groups whose AVG(salary) exceeds a scalar subquery computing the company-wide AVG(salary). WHERE filters rows before grouping, so a condition on an aggregate must go in HAVING. Note the company average is over employees, not an average of department averages.

Original UniCircle question. Last reviewed 21 Oct 2026.