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 a.name AS first, b.name AS second, a.dept_id, a.salary
FROM employees a JOIN employees b
ON b.dept_id = a.dept_id AND b.salary = a.salary AND a.id < b.id
ORDER BY a.dept_id, first;| first | second | dept_id | salary |
|---|---|---|---|
| Ravi | Mei | 1 | 95000 |
Ravi and Mei both earn 95000 in Engineering. Using a.id <> b.id instead would return the pair twice, once in each order:
sql
SELECT a.name AS first, b.name AS second
FROM employees a JOIN employees b
ON b.dept_id = a.dept_id AND b.salary = a.salary AND a.id <> b.id
ORDER BY first;| first | second |
|---|---|
| Mei | Ravi |
| Ravi | Mei |
NULLs drop out naturally: Lena has no department and no salary, and NULL = NULL is not true, so she never pairs with anyone. If NULL departments should match each other, use IS in SQLite or IS NOT DISTINCT FROM in PostgreSQL.
The same a.id < b.id pattern produces unique pairs for any "find matching pairs" puzzle, such as possible duplicate customers or overlapping bookings.
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 a.name AS first, b.name AS second, a.dept_id, a.salary
FROM employees a JOIN employees b
ON b.dept_id = a.dept_id AND b.salary = a.salary AND a.id < b.id
ORDER BY a.dept_id, first;
SELECT a.name AS first, b.name AS second
FROM employees a JOIN employees b
ON b.dept_id = a.dept_id AND b.salary = a.salary AND a.id <> b.id
ORDER BY first;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Self join on the matching columns
- a.id < b.id removes self and mirror pairs
- <> would return each pair twice
- NULLs do not match with =
Common mistakes
- Using <> and doubling the result
- Forgetting the department condition and pairing across departments
Follow-ups
- How would you find groups of three or more with the same salary?
- How would you find overlapping date ranges?
Tips
- Explain why < and not <>