← Back to SQL, page 5

sql-0100 · Medium mid

Write a query for departments whose average salary is higher than the company-wide average.

Group employees by department and compare the group's AVG(salary) with a scalar subquery for the overall average in HAVING: HAVING AVG(salary) > (SELECT AVG(salary) FROM employees). The subquery runs once; the comparison happens per group.

Full answer

The condition is about each department's group, so it belongs in HAVING, and the company figure comes from a scalar subquery.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT ROUND(AVG(salary), 2) AS company_avg FROM employees;
company_avg
77857.14
sql
SELECT d.name, ROUND(AVG(e.salary), 2) AS dept_avg
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
HAVING AVG(e.salary) > (SELECT AVG(salary) FROM employees)
ORDER BY dept_avg DESC;
namedept_avg
Engineering103333.33

Only Engineering is above the overall average of 77857.14. Sales and Support are below it.

Two choices worth stating. Lena's NULL salary is ignored by both averages, and she has no department, so her NULL group has a NULL average and drops out (NULL > anything is UNKNOWN). And the comparison is between department averages and the employee-weighted company average. If the question meant "above the average department", you would compare with the average of department averages instead, which can give a different list.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT ROUND(AVG(salary), 2) AS company_avg FROM employees;

SELECT d.name, ROUND(AVG(e.salary), 2) AS dept_avg
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
HAVING AVG(e.salary) > (SELECT AVG(salary) FROM employees)
ORDER BY dept_avg DESC;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Per-group conditions go in HAVING
  • A scalar subquery supplies the company-wide value
  • NULL groups drop out of the comparison
  • Clarify employee-weighted versus department-weighted averages

Common mistakes

  • Putting the aggregate comparison in WHERE
  • Comparing with the average of department averages without saying so

Follow-ups

  • How would you show the difference from the company average for every department?
  • How would you write this with a window function?

Tips

  • Ask which average the business means before writing the query