← Back to SQL, page 15

sql-0294 · Hard senior

How can a window compare each employee's salary with the average of their colleagues, excluding themselves?

Use a full-partition frame with EXCLUDE CURRENT ROW: AVG(salary) OVER (PARTITION BY dept_id ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW). SQLite 3.28+ and PostgreSQL 11+ support EXCLUDE. The arithmetic alternative is (SUM - salary) / (COUNT - 1).

Full answer
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 name, dept_id, salary,
       AVG(salary) OVER (PARTITION BY dept_id ORDER BY id
         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) AS peers_avg
FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, id;
namedept_idsalarypeers_avg
Asha112000095000.0
Ravi195000107500.0
Mei195000107500.0
Tom27000062000.0
Sara26200070000.0
Diego35500048000.0
Omar34800055000.0

For Asha, the colleagues' average is the mean of Ravi and Mei, 95000. For Tom, it is Sara's 62000 alone. EXCLUDE CURRENT ROW removes only the current row from the frame; EXCLUDE TIES would also remove peers with equal ORDER BY values, and EXCLUDE GROUP both.

The arithmetic form works on any engine with window aggregates:

sql
SELECT name, salary,
       CASE WHEN COUNT(*) OVER (PARTITION BY dept_id) > 1
            THEN (SUM(salary) OVER (PARTITION BY dept_id) - salary) * 1.0 / (COUNT(*) OVER (PARTITION BY dept_id) - 1) END AS peers_avg
FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, id;
namesalarypeers_avg
Asha12000095000.0
Ravi95000107500.0
Mei95000107500.0
Tom7000062000.0
Sara6200070000.0
Diego5500048000.0
Omar4800055000.0

The CASE avoids dividing by zero for a department of one, which would have no colleagues.

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 name, dept_id, salary,
       AVG(salary) OVER (PARTITION BY dept_id ORDER BY id
         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) AS peers_avg
FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, id;

SELECT name, salary,
       CASE WHEN COUNT(*) OVER (PARTITION BY dept_id) > 1
            THEN (SUM(salary) OVER (PARTITION BY dept_id) - salary) * 1.0 / (COUNT(*) OVER (PARTITION BY dept_id) - 1) END AS peers_avg
FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • EXCLUDE CURRENT ROW drops the current row from the frame
  • Needs an explicit frame such as the whole partition
  • (SUM - x) / (COUNT - 1) is a portable alternative
  • Guard single-member partitions

Common mistakes

  • Comparing with an average that includes the person themselves
  • Dividing by COUNT - 1 when the count is 1

Follow-ups

  • What do EXCLUDE TIES and EXCLUDE GROUP do?
  • How would you flag people more than 10 percent above peers?

Tips

  • Mention the single-member edge case