
At Meta, the Facebook Friends graph team wants to identify users whose first friendship creation happened within 7 days of each other. This can be useful when analyzing early-network formation patterns around surfaces like Facebook Groups and downstream AARRR activation behavior.
Write a SQL query to find pairs of users who became friends within 7 days of each other.
user_id_1 < user_id_2.days_apart, then user_id_1, then user_id_2.friendships| column | type | description |
|---|---|---|
| friendship_id | INT | Unique friendship event ID |
| user_id_1 | INT | One side of the friendship edge |
| user_id_2 | INT | Other side of the friendship edge |
| became_friends_at | TIMESTAMP | Timestamp when the friendship was created |
| surface | VARCHAR(50) | Meta surface where the connection was initiated |
| friendship_id | user_id_1 | user_id_2 | became_friends_at | surface |
|---|---|---|---|---|
| 1008 | 108 | 101 | 2024-01-08 11:00:00 | fb_groups |
| 1002 | 102 | 103 | 2024-01-03 09:30:00 | reels |
| 1005 | 105 | 106 | 2024-01-10 18:20:00 | ig_save |
| 1001 | 101 | 102 | 2024-01-01 10:00:00 | fb_groups |
| 1009 | 109 | 110 | NULL | reels |
| 1004 | 104 | 105 | 2024-01-09 15:00:00 | fb_groups |
| user_id_1 | user_id_2 | first_friendship_at_1 | first_friendship_at_2 | days_apart |
|---|---|---|---|---|
| 101 | 102 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 0 |
| 103 | 104 | 2024-01-03 09:30:00 | 2024-01-03 09:30:00 | 0 |
| 105 | 106 | 2024-01-09 15:00:00 | 2024-01-10 18:20:00 | 1 |
| Column | Type | Description |
|---|---|---|
| friendship_idPK | INT | Unique friendship event identifier |
| user_id_1 | INT | First user in the friendship edge |
| user_id_2 | INT | Second user in the friendship edge |
| became_friends_at | TIMESTAMP | Timestamp when the friendship was created |
| surface | VARCHAR(50) | Meta product surface where the friendship was initiated |
| friendship_id | user_id_1 | user_id_2 | became_friends_at | surface |
|---|---|---|---|---|
| 1008 | 108 | 101 | 2024-01-08 11:00:00 | fb_groups |
| 1002 | 102 | 103 | 2024-01-03 09:30:00 | reels |
| 1005 | 105 | 106 | 2024-01-10 18:20:00 | ig_save |
| 1001 | 101 | 102 | 2024-01-01 10:00:00 | fb_groups |
| 1009 | 109 | 110 | reels | |
| 1004 | 104 | 105 | 2024-01-09 15:00:00 | fb_groups |
| 1007 | 107 | 108 | 2024-01-20 12:00:00 | reels |
| 1003 | 103 | 104 | 2024-01-03 09:30:00 | ig_save |
| 1010 | 111 | 112 | 2024-02-01 08:00:00 | |
| 1006 | 106 | 107 | 2024-01-18 07:45:00 | |
| 1011 | 105 | 109 | 2024-01-07 23:10:52 | qr_invite |
| 1012 | 108 | 107 | 2024-02-02 22:57:55 | stories |
| 1013 | 102 | 106 | 2024-01-09 09:41:19 | contact_import |
| 1014 | 111 | 106 | 2024-01-07 12:11:04 | live_video |
| 1015 | 108 | 109 | 2024-02-04 07:45:48 | ig_reels |
| 1016 | 105 | 105 | 2023-12-31 21:43:21 | marketplace |
| 1017 | 103 | 114 | 2023-12-31 03:11:49 | qr_invite |
| 1018 | 111 | 108 | 2024-02-04 01:34:50 | null |
| 1019 | 100 | 99 | 2024-02-01 10:47:21 | facebook_search |
| 1020 | 101 | 110 | null | messenger |
| 1021 | 107 | 107 | 2024-01-05 22:12:24 | null |
| 1022 | 108 | 114 | 2024-01-13 21:50:08 | suggested_friends |
| 1023 | 99 | 109 | 2024-02-04 07:33:04 | people_you_may_know |
| 1024 | 105 | 99 | 2024-01-21 12:25:04 | threads_share |
| 1025 | 106 | 101 | 2024-01-08 06:58:02 | profile_visit |
| 1026 | 107 | 106 | 2023-12-30 21:01:04 | notifications |
| 1027 | 112 | 110 | 2023-12-30 23:52:25 | ig_reels |
| 1028 | 102 | 105 | 2024-01-08 23:50:51 | fb_groups |
| user_id_1 | user_id_2 | first_friendship_at_1 | first_friendship_at_2 | days_apart |
|---|---|---|---|---|
| 101 | 102 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 0 |
| 103 | 105 | 2023-12-31 03:11:49 | 2023-12-31 21:43:21 | 0 |
| 103 | 114 | 2023-12-31 03:11:49 | 2023-12-31 03:11:49 | 0 |
| 105 | 114 | 2023-12-31 21:43:21 | 2023-12-31 03:11:49 | 0 |
| 106 | 107 | 2023-12-30 21:01:04 | 2023-12-30 21:01:04 | 0 |
| 106 | 110 | 2023-12-30 21:01:04 | 2023-12-30 23:52:25 | 0 |
| 106 | 112 | 2023-12-30 21:01:04 | 2023-12-30 23:52:25 | 0 |
| 107 | 110 | 2023-12-30 21:01:04 | 2023-12-30 23:52:25 | 0 |
| 107 | 112 | 2023-12-30 21:01:04 | 2023-12-30 23:52:25 | 0 |
| 109 | 111 | 2024-01-07 23:10:52 | 2024-01-07 12:11:04 | 0 |
| 110 | 112 | 2023-12-30 23:52:25 | 2023-12-30 23:52:25 | 0 |
| 101 | 103 | 2024-01-01 10:00:00 | 2023-12-31 03:11:49 | 1 |
| 101 | 105 | 2024-01-01 10:00:00 | 2023-12-31 21:43:21 | 1 |
| 101 | 114 | 2024-01-01 10:00:00 | 2023-12-31 03:11:49 | 1 |
| 102 | 103 | 2024-01-01 10:00:00 | 2023-12-31 03:11:49 | 1 |
| 102 | 105 | 2024-01-01 10:00:00 | 2023-12-31 21:43:21 | 1 |
| 102 | 114 | 2024-01-01 10:00:00 | 2023-12-31 03:11:49 | 1 |
| 103 | 106 | 2023-12-31 03:11:49 | 2023-12-30 21:01:04 | 1 |
| 103 | 107 | 2023-12-31 03:11:49 | 2023-12-30 21:01:04 | 1 |
| 103 | 110 | 2023-12-31 03:11:49 | 2023-12-30 23:52:25 | 1 |
| 103 | 112 | 2023-12-31 03:11:49 | 2023-12-30 23:52:25 | 1 |
| 105 | 106 | 2023-12-31 21:43:21 | 2023-12-30 21:01:04 | 1 |
| 105 | 107 | 2023-12-31 21:43:21 | 2023-12-30 21:01:04 | 1 |
| 105 | 110 | 2023-12-31 21:43:21 | 2023-12-30 23:52:25 | 1 |
| 105 | 112 | 2023-12-31 21:43:21 | 2023-12-30 23:52:25 | 1 |
| 106 | 114 | 2023-12-30 21:01:04 | 2023-12-31 03:11:49 | 1 |
| 107 | 114 | 2023-12-30 21:01:04 | 2023-12-31 03:11:49 | 1 |
| 108 | 109 | 2024-01-08 11:00:00 | 2024-01-07 23:10:52 | 1 |
| 108 | 111 | 2024-01-08 11:00:00 | 2024-01-07 12:11:04 | 1 |
| 110 | 114 | 2023-12-30 23:52:25 | 2023-12-31 03:11:49 | 1 |
| 112 | 114 | 2023-12-30 23:52:25 | 2023-12-31 03:11:49 | 1 |
| 101 | 104 | 2024-01-01 10:00:00 | 2024-01-03 09:30:00 | 2 |
| 101 | 106 | 2024-01-01 10:00:00 | 2023-12-30 21:01:04 | 2 |
| 101 | 107 | 2024-01-01 10:00:00 | 2023-12-30 21:01:04 | 2 |
| 101 | 110 | 2024-01-01 10:00:00 | 2023-12-30 23:52:25 | 2 |
| 101 | 112 | 2024-01-01 10:00:00 | 2023-12-30 23:52:25 | 2 |
| 102 | 104 | 2024-01-01 10:00:00 | 2024-01-03 09:30:00 | 2 |
| 102 | 106 | 2024-01-01 10:00:00 | 2023-12-30 21:01:04 | 2 |
| 102 | 107 | 2024-01-01 10:00:00 | 2023-12-30 21:01:04 | 2 |
| 102 | 110 | 2024-01-01 10:00:00 | 2023-12-30 23:52:25 | 2 |
| 102 | 112 | 2024-01-01 10:00:00 | 2023-12-30 23:52:25 | 2 |
| 103 | 104 | 2023-12-31 03:11:49 | 2024-01-03 09:30:00 | 3 |
| 104 | 105 | 2024-01-03 09:30:00 | 2023-12-31 21:43:21 | 3 |
| 104 | 114 | 2024-01-03 09:30:00 | 2023-12-31 03:11:49 | 3 |
| 104 | 106 | 2024-01-03 09:30:00 | 2023-12-30 21:01:04 | 4 |
| 104 | 107 | 2024-01-03 09:30:00 | 2023-12-30 21:01:04 | 4 |
| 104 | 109 | 2024-01-03 09:30:00 | 2024-01-07 23:10:52 | 4 |
| 104 | 110 | 2024-01-03 09:30:00 | 2023-12-30 23:52:25 | 4 |
| 104 | 111 | 2024-01-03 09:30:00 | 2024-01-07 12:11:04 | 4 |
| 104 | 112 | 2024-01-03 09:30:00 | 2023-12-30 23:52:25 | 4 |
| 104 | 108 | 2024-01-03 09:30:00 | 2024-01-08 11:00:00 | 5 |
| 101 | 109 | 2024-01-01 10:00:00 | 2024-01-07 23:10:52 | 6 |
| 101 | 111 | 2024-01-01 10:00:00 | 2024-01-07 12:11:04 | 6 |
| 102 | 109 | 2024-01-01 10:00:00 | 2024-01-07 23:10:52 | 6 |
| 102 | 111 | 2024-01-01 10:00:00 | 2024-01-07 12:11:04 | 6 |
| 101 | 108 | 2024-01-01 10:00:00 | 2024-01-08 11:00:00 | 7 |
| 102 | 108 | 2024-01-01 10:00:00 | 2024-01-08 11:00:00 | 7 |
| 103 | 109 | 2023-12-31 03:11:49 | 2024-01-07 23:10:52 | 7 |
| 103 | 111 | 2023-12-31 03:11:49 | 2024-01-07 12:11:04 | 7 |
| 105 | 109 | 2023-12-31 21:43:21 | 2024-01-07 23:10:52 | 7 |
| 105 | 111 | 2023-12-31 21:43:21 | 2024-01-07 12:11:04 | 7 |
| 109 | 114 | 2024-01-07 23:10:52 | 2023-12-31 03:11:49 | 7 |
| 111 | 114 | 2024-01-07 12:11:04 | 2023-12-31 03:11:49 | 7 |