Table Schema
Inspect
Table
| Column | Type |
|---|---|
| user_id | integer |
| friend_id | integer |
Find users who have more than 3 friends.
Table Schema
| Column | Type |
|---|---|
| user_id | integer |
| friend_id | integer |
Sample Data
| user_id | friend_id |
|---|---|
| 101 | 102 |
| 101 | 103 |
| 101 | 104 |
| 101 | 105 |
| 102 | 101 |
| 102 | 103 |
| 103 | 101 |
| user_id |
|---|
| 101 |
SQL Editor
| user_id |
|---|
| 101 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT user_id FROM friends GROUP BY user_id HAVING COUNT(DISTINCT friend_id) > 3;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions