Sub topic 6
Comparing Rows: Pairs Without Duplicates or Mirror Images
Prerequisites
Assume you know basic SELECT, INNER JOIN, LEFT JOIN, primary keys, foreign keys, and NULL handling. This chapter uses the employees table from the earlier examples:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INT REFERENCES employees(id)
);
INSERT INTO employees (id, name, manager_id) VALUES
(1, 'Ava', NULL),
(2, 'Ben', 1),
(3, 'Chen', 1),
(4, 'Dana', 2);Treating one table as two
A self join compares rows from one table with other rows in that same table. SQL needs two names for the table so that it can tell the two row roles apart.
SELECT
a.name AS first_employee,
b.name AS second_employee
FROM employees AS a
INNER JOIN employees AS b
ON a.manager_id = b.manager_id
AND a.manager_id IS NOT NULL
AND a.id < b.id;Here, a and b are table aliases. The aliases do not create new tables. They give the same table two roles:
arepresents the first employee in a pair.brepresents the second employee in a pair.
The condition a.manager_id = b.manager_id finds employees who report to the same manager. In the sample data, the result is Ben and Chen because both report to Ava.
Preventing self-pairs
A self join can match a row with itself. For example, without an ID condition, Ben could match Ben:
| first_employee | second_employee |
|---|---|
| Ben | Ben |
Usually, a comparison should not include this pair. The condition a.id < b.id prevents it because an ID cannot be less than itself.
The condition also has another useful effect. It keeps only one ordering of each pair:
a.id = 2andb.id = 3is kept.a.id = 3andb.id = 2is rejected.
These two rows describe the same unordered pair, so returning both would be a mirror duplicate.
Why other conditions fail
The following conditions have different problems:
a.id = b.idreturns only self-pairs.a.id <> b.idremoves self-pairs but keeps both mirror images.a.id <= b.idremoves mirror images but keeps self-pairs.- No ID condition allows self-pairs and mirror images.
Use a strict ordering condition such as a.id < b.id when each pair should appear exactly once.
Comparing rows by another rule
The matching condition does not have to use a parent relationship. You can compare rows that share a category, location, or another attribute.
For example, this query finds every unique pair of employees with the same manager:
SELECT
a.name AS first_employee,
b.name AS second_employee,
a.manager_id
FROM employees AS a
INNER JOIN employees AS b
ON a.manager_id = b.manager_id
AND a.manager_id IS NOT NULL
AND a.id < b.id
ORDER BY a.manager_id, a.id;The shared manager condition identifies candidate pairs. The ID condition selects one direction and rejects self-matches.
If you compare rows using a nullable column, remember that NULL = NULL is not true in SQL. Two rows with no manager will not match through a.manager_id = b.manager_id. That behavior is often correct for hierarchy comparisons, but it may not be correct for every business rule.
Checking the result
For n eligible rows in one group, the maximum number of unique pairs is:
n × (n - 1) / 2
A group of four rows can produce six pairs. This formula helps you notice unexpected duplicates or missing matches.
Exercise
What condition should you add to a self join if you want each pair once, with no row paired with itself?
Show answer
Add a strict ordering condition such as a.id < b.id. It removes self-pairs and mirror duplicates.