101. Unique Friendships

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.

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…