← Back to SQL, page 3

sql-0053 · Medium mid

List every pair of students who live in the same city, with each pair shown only once.

Self join students on a.city = b.city and add a.id < b.id. The inequality removes pairs of a student with themselves and keeps only one of the two orderings (Asha-Chen but not Chen-Asha). Students with a NULL city never pair, because NULL = NULL is not true.

Full answer

A self join on city alone produces too much:

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);
sql
SELECT a.name AS a, b.name AS b, a.city
FROM students a
JOIN students b ON a.city = b.city
ORDER BY a.city, a.name, b.name;
abcity
BenBenLeeds
BenEliLeeds
EliBenLeeds
EliEliLeeds
AshaAshaPune
AshaChenPune
ChenAshaPune
ChenChenPune

Each student is paired with themselves, and each real pair appears twice in both orders. Using a.id <> b.id would remove the self-pairs but keep both orders. Using a.id < b.id keeps exactly one ordering of each pair:

sql
SELECT a.name AS a, b.name AS b, a.city
FROM students a
JOIN students b ON a.city = b.city AND a.id < b.id
ORDER BY a.city, a.name;
abcity
BenEliLeeds
AshaChenPune

Dara has a NULL city, so a.city = b.city is never true for her, and she pairs with nobody. That is usually right: an unknown city should not count as the same city.

This pattern, a self join with < on the key, is the general tool for "all unique pairs" problems: roommates, matches between teams, or duplicate candidates that need a human check. For n rows in one group it produces n(n-1)/2 pairs, so on a large group it grows quickly.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT a.name AS a, b.name AS b, a.city
FROM students a
JOIN students b ON a.city = b.city
ORDER BY a.city, a.name, b.name;

SELECT a.name AS a, b.name AS b, a.city
FROM students a
JOIN students b ON a.city = b.city AND a.id < b.id
ORDER BY a.city, a.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Self join on the shared attribute
  • a.id < b.id removes self-pairs and mirrored duplicates
  • NULL values never pair with =
  • Pair count grows as n(n-1)/2 per group

Common mistakes

  • Using <> and getting every pair twice
  • Forgetting the inequality and pairing rows with themselves

Follow-ups

  • How would you count the pairs per city without listing them?
  • How would you find pairs of students who share at least two courses?

Tips

  • Show the row count before and after adding a.id < b.id