← Back to SQL, page 5

sql-0092 · Easy fresher

Write a query that lists employees who earn more than the company's average salary.

Compute the average in a scalar subquery and compare each salary with it: WHERE salary > (SELECT AVG(salary) FROM employees). A scalar subquery returns one value, so it can be used anywhere a single value is allowed. An aggregate cannot go directly in WHERE, which is why the subquery is needed.

Full answer

The average is one number for the whole table, but WHERE looks at one row at a time and cannot contain an aggregate. A scalar subquery bridges the gap: it runs on its own, returns a single value, and the outer query compares against it.

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 name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
ORDER BY salary DESC, name;
namesalary
Asha120000
Mei95000
Ravi95000

AVG skips Lena's NULL salary, so the average is over seven known salaries. Lena is not in the result either, because NULL > 77857.14... is UNKNOWN.

This subquery does not depend on the outer row, so the database computes it once. Contrast that with a correlated subquery, which refers to the outer row and is logically evaluated per row; that is the tool for "above their own department's average".

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 name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
ORDER BY salary DESC, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • A scalar subquery returns one value
  • It lets WHERE compare against an aggregate
  • An uncorrelated subquery is evaluated once
  • AVG ignores NULL; NULL salaries also fail the comparison

Common mistakes

  • Writing WHERE salary > AVG(salary)
  • Forgetting that NULL salaries change the average's denominator

Follow-ups

  • How would you show each employee's difference from the average?
  • How would you compare against the department average instead?

Tips

  • Show the average itself first, so the result can be checked by eye