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;| name | dept_id | salary | peers_avg |
|---|---|---|---|
| Asha | 1 | 120000 | 95000.0 |
| Ravi | 1 | 95000 | 107500.0 |
| Mei | 1 | 95000 | 107500.0 |
| Tom | 2 | 70000 | 62000.0 |
| Sara | 2 | 62000 | 70000.0 |
| Diego | 3 | 55000 | 48000.0 |
| Omar | 3 | 48000 | 55000.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;| name | salary | peers_avg |
|---|---|---|
| Asha | 120000 | 95000.0 |
| Ravi | 95000 | 107500.0 |
| Mei | 95000 | 107500.0 |
| Tom | 70000 | 62000.0 |
| Sara | 62000 | 70000.0 |
| Diego | 55000 | 48000.0 |
| Omar | 48000 | 55000.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