Accepted Friend Requests
Show all confirmed friendships with both users' full names and when the friendship was accepted.
- Joins
- Date analysis
- Filtering
- Sorting
Challenge brief
Understand the request
Social Graph Team is building a 'Friends Since' feature and needs a list of all confirmed friendships with their acceptance dates.
List accepted friendships with friend1_name, friend2_name, and accepted_at date.
Return
- friend1_name (full name)
- friend2_name (full name)
- accepted_at
Constraints
- Return accepted friendships only
- Show the most recently accepted friendships first
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
friendships
friendship_idINTEGERuser_id_1INTEGERuser_id_2INTEGERstatusVARCHAR(20)accepted_atDATETIME
users
user_idINTEGERfirst_nameVARCHAR(50)last_nameVARCHAR(50)
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
The friendships table stores connections. The status must be 'accepted'. Both user_id_1 and user_id_2 reference users — join the users table twice with different aliases for each side.
Hint 2
JOIN friendships to users twice: u1 ON user_id_1 = user_id, u2 ON user_id_2 = user_id. WHERE f.status = 'accepted'. Concatenate names with || ' ' ||.
Hint 3
Start with the tables that establish the result grain for question 4, select the required output aliases, and add the remaining joins, filters, aggregation, and ordering one clause at a time.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT u1.first_name || ' ' || u1.last_name AS friend1_name, u2.first_name || ' ' || u2.last_name AS friend2_name, f.accepted_at FROM friendships f INNER JOIN users u1 ON f.user_id_1 = u1.user_id INNER JOIN users u2 ON f.user_id_2 = u2.user_id WHERE f.status = 'accepted' ORDER BY f.accepted_at DESC;Why this works
friendship_id 5 (John to Sarah) has status = 'pending' and is excluded. The remaining 4 are 'accepted'. Two aliases (u1, u2) on the users table let you pull each person's name independently.
Success check
4 accepted friendships — most recent is Mike Wilson and Sarah Jones (May 2020)
Expected result
Use this output to verify values, aliases, ordering, and row count.
| friend1_name | friend2_name | accepted_at |
|---|---|---|
| Mike Wilson | Sarah Jones | 2020-05-15 13:00:00 |
| Jane Smith | Sarah Jones | 2020-04-10 10:00:00 |
| John Doe | Mike Wilson | 2020-03-25 15:30:00 |
| John Doe | Jane Smith | 2020-02-20 12:00:00 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Netflix
Independent Netflix-style streaming, subscription, catalog, ratings, and engagement SQL practice.
Airbnb
Independent Airbnb-style marketplace, booking, listing, payment, review, and guest analytics SQL practice.
Return to the complete interview preparation experience.