Menu
Coddy logo textTech

Recap - Self Join

Part of the Fundamentals section of Coddy's SQL journey — lesson 47 of 72.

challenge icon

Challenge

Hard

Available tables and columns:

  • <strong>friends</strong>: <strong>id</strong>, <strong>name</strong>, <strong>friend_id</strong>

For each person in the friends table, find all the friend-of-friend connections they have. A friend-of-friend connection occurs when person A is friends with person B, who is in turn friends with person C. List these connections as three columns:

  • friend1: the starting person
  • friend2: their direct friend
  • friend3: their friend's friend

Rules:

  • Don't include cases where friend3 is the same person as friend1
  • Order the results by friend1's name in descending order

Example: If Alice is friends with Bob, and Bob is friends with Carol, then Alice-Bob-Carol would be a valid friend-of-friend connection.

Try it yourself

All lessons in Fundamentals