Company: Meta | Difficulty: easy
Meta's growth team wants to see how active each user is at sending friend requests.
Write a query to find, for every user who has sent at least one friend request, their name and the total number of friend requests they've sent (regardless of whether the request was accepted, pending, or declined) as total_requests_sent. Sort the results by total_requests_sent in descending order, then by name in ascending alphabetical order for ties.
name
total_requests_sent
users
user_id
friend_requests
request_id
sender_id
status
request_date
Mark Zuckerberg sent 2 requests (101 and 102), Sheryl Sandberg sent 1 request (103), and Chris Cox sent none, so he doesn't appear in the output at all. Sorted by count descending, Mark Zuckerberg (2) comes before Sheryl Sandberg (1).
Mark Zuckerberg
Sheryl Sandberg
Chris Cox
DROP TABLE IF EXISTS friend_requests; DROP TABLE IF EXISTS users; CREATE TABLE users (user_id INT, name VARCHAR(255)); CREATE TABLE friend_requests (request_id INT, sender_id INT, status VARCHAR(50), request_date DATE); INSERT INTO users VALUES (1, 'Mark Zuckerberg'), (2, 'Sheryl Sandberg'), (3, 'Chris Cox'); INSERT INTO friend_requests VALUES (101, 1, 'accepted', '2023-01-01'), (102, 1, 'pending', '2023-01-05'), (103, 2, 'accepted', '2023-02-01');
← All SQL Challenges · Meta Questions · Learn SQL Free