Recap - Self Join
Part of the Fundamentals section of Coddy's SQL journey — lesson 47 of 72.
Challenge
HardAvailable 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
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns