Friend Count
For this challenge, suppose we are working on an app similar to Facebook Messenger, called "Footbook Messenger". You're a data scientist studying social graphs. You want to cluster users by their social behavior, and compare users by how many friends they have.
The users are in the fb_users
table, and their friendships are in friendships
.
column_name | type |
---|---|
id | INTEGER |
name | VARCHAR |
username | VARCHAR |
email | VARCHAR |
phone_number | VARCHAR |
column_name | type | reference |
---|---|---|
friend_id_1 | INTEGER | fb_users.id |
friend_id_2 | INTEGER | fb_users.id |
When 2 users become friends, 2 rows in the
friendships
table get created, i.e. when Bob befriends Alice, there is 1 row where Bob's ID isfriend_id_1
and Alice's isfriend_id_2
, and another row where Alice's ID isfriend_id_1
and Bob's isfriend_id_2
.
Your task is to determine the distribution of friend counts, e.g. how many people have 5 friends, how many have 4 friends, etc. If you visualized it with a bar graph, it would look something like this:
Select the friend count, aliased to friend_count
, and the number of users with that friend count,
aliased it to users_having_friend_count
. Order by the friend_count
.