Pending Friend Requests
Which friend requests are still waiting to be accepted?
- Joins
- Filtering
- Sorting
Challenge brief
Understand the request
Social Graph Team is monitoring unresolved connection requests to improve the friend-suggestion experience.
List pending friend requests showing sender name, receiver name, and when the request was sent.
Return
- sender_name (full name)
- receiver_name (full name)
- requested_at
Constraints
- Return friendship requests whose status is 'pending'
- Show the oldest pending requests 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)requested_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 both accepted and pending connections. Filter to 'pending'. User names are in users — join it twice with different aliases for sender and receiver.
Hint 2
JOIN friendships to users twice: u1 ON user_id_1, u2 ON user_id_2. WHERE f.status = 'pending'. Concatenate names with || ' ' ||.
Hint 3
Start with the tables that establish the result grain for question 17, 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 sender_name, u2.first_name || ' ' || u2.last_name AS receiver_name, f.requested_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 = 'pending' ORDER BY f.requested_at, f.friendship_idWhy this works
The friendships table uses two FK columns (user_id_1, user_id_2) that both reference users. Two aliases (u1, u2) let you reach each side independently. Only friendship_id 5 (John to Sarah) has status = 'pending'.
Success check
1 pending request — John Doe sent to Sarah Jones (2024-01-18)
Expected result
Use this output to verify values, aliases, ordering, and row count.
| sender_name | receiver_name | requested_at |
|---|---|---|
| John Doe | Sarah Jones | 2024-01-18 10: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.