This advanced SQL exercise is part of the book: Advanced SQL – Practical Techniques and Use-Cases, which includes 100 day-to-day SQL challenges (and solutions), need-to-know advanced features, and key concepts.

Interesting question I’ve recently encoutered. Consider the following table:

CREATE TABLE friendships
(friendship_id int IDENTITY(1,1) PRIMARY KEY,
 member_id_1 int, 
 member_id_2 int)

The Friendships table describes relationship between different users, so for example we
can see that user_id 1 is a friend of user_id 2, user_id 3 is a friend of user_id 2, and so on.