BeginnerCoder

Lesson 21 — Self join: finding pairs

Self join for pairs

Lesson 15 joined each row to one related row (its manager). A self join can also build pairs: every combination of two rows that share something. Joining products to themselves ON the same category gives all pairs — but also each product with itself, and every pair twice (A–B and B–A). The fix is a.id < b.id: each pair appears once, and never a product with itself.

FROM players x JOIN players y
  ON x.club = y.club AND x.id < y.id

Why it matters: "items often compared", "people in the same city", "possible duplicate records" — pair-finding is a daily data-cleaning job.

Want it another way?
Task

Extend the ON condition so each pair appears only once and no product is paired with itself. Expected: exactly 4 unique pairs, no self-pairs.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with SQL Deep Dive

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.