Meta's 'People You May Know' feature suggests two users to each other when they are not yet friends but share at least 2 mutual friends. The `friendships` table stores each friendship once (as a single row, in either direction), and friendship is symmetric. Write an SQL query that returns each recommended pair once, as `user_id`, `recommended_user_id` (with `user_id` smaller than `recommended_user_id`) and `mutual_friends` (the number of friends they have in common). Order the results by `mutual_friends` descending, then `user_id` ascending, then `recommended_user_id` ascending.
Loading…
Try solving the question with the schema explorer and hints first. When ready, reveal the verified canonical query and step-by-step logic breakdown.
Ask questions, share alternate queries, and explore solutions
Your recent query executions and answer checks
This will insert the verified MySQL solution into your editor so you can study how each clause transforms the table.