The core join isn't bad but the unordered pair thing tripped me up for a minute.
Start by clarifying the schema and assumptions (e.g., date granularity, friendship bidirectionality). Then, use a self-join on the listens table to find pairs of users who listened to the same song on the same day, count distinct songs per pair, filter for >3, and finally exclude existing friendships. Ensure the query is efficient by using appropriate joins and filters.
Pro tip: Mention that you would consider the scalability of the query, especially for large datasets, and discuss potential optimizations like indexing or pre-aggregation. Also, clarify whether the recommendation should be symmetric (i.e., if A and B are recommended, B and A is the same) and handle it accordingly.
Ask about the schema, date format, and whether friendships are bidirectional. Confirm that 'same day' means the same calendar date and that we need distinct songs.
Self-join the listens table on song_id and listen_date, ensuring user_id1 < user_id2 to avoid duplicates and self-pairs. This gives all pairs who listened to the same song on the same day.
Group by the user pair and count distinct song_ids. Filter to keep only pairs with more than 3 shared songs.
Left join the friendships table (considering both directions) and filter out pairs where a friendship exists.
Select the final user pairs, ensuring each pair appears only once (e.g., by ordering user IDs).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.