Meta: Going and Interested Counts per Facebook Event
The Facebook Events team shows hosts a small dashboard with how many people answered Going and how many Interested.
- Difficulty: Easy
- Topics: Conditional Logic, Aggregation, Joins
- Dialect: MySQL
- Problem: #750
Problem statement
The Facebook Events team shows hosts a small dashboard with how many people answered Going and how many Interested.
Return one row for every event with the columns event_id, event_name, going (the number of going responses) and interested (the number of interested responses). declined responses count in neither column, and an event nobody answered shows 0 and 0. Return the rows in any order.
A practice scenario set at Meta: the tables and data are invented, and this is not a question from, or endorsed by, Meta.
Tables
Table: FbEvent
| Column | Type |
|---|---|
| event_id | int |
| event_name | varchar |
| starts_at | datetime |
Primary key: event_id.
One row per event created on Facebook.
Table: EventRsvp
| Column | Type |
|---|---|
| event_id | int |
| user_id | int |
| response | enum(going, interested, declined) |
Primary key: event_id, user_id.
A user's current answer to an event's invitation — one row per (event, user).
Examples
Example 1
FbEvent
| event_id | event_name | starts_at |
|---|---|---|
| 1 | Bengaluru Runners Meetup | 2025-07-06 06:00:00 |
| 2 | Indie Music Night | 2025-07-12 19:30:00 |
| 3 | Startup Pitch Day | 2025-07-19 10:00:00 |
| 4 | Diwali Craft Fair | 2025-10-18 11:00:00 |
EventRsvp
| event_id | user_id | response |
|---|---|---|
| 1 | 10 | going |
| 1 | 11 | going |
| 1 | 12 | interested |
| 1 | 13 | declined |
| 1 | 14 | going |
| 2 | 10 | interested |
| 2 | 14 | interested |
| 3 | 15 | declined |
Output
| event_id | event_name | going | interested |
|---|---|---|---|
| 1 | Bengaluru Runners Meetup | 3 | 1 |
| 2 | Indie Music Night | 0 | 2 |
| 3 | Startup Pitch Day | 0 | 0 |
| 4 | Diwali Craft Fair | 0 | 0 |
How to solve Meta: Going and Interested Counts per Facebook Event
Both numbers are counts over the same set of rows — one event's responses — split by the value of response. That is conditional aggregation: group once per event and let SUM(CASE WHEN response = 'going' THEN 1 ELSE 0 END) count only the matching rows, with a second CASE for interested. Declined answers fall into the ELSE 0 of both and add nothing.
Events with no answers must still be listed, so the join is a LEFT JOIN from FbEvent. Such an event gets one row with NULL response; the CASE turns it into 0 for both columns, so the sums are 0 rather than NULL. COUNT(CASE WHEN … THEN 1 END) is the same idea — COUNT skips the NULLs the CASE produces and returns 0 for an empty group.
Correlated COUNT(*) subqueries per event are the third option; they read the responses twice per event but say each number on its own. With an index on EventRsvp(event_id) all of them are a single pass over the responses.
Reference solution (MySQL)
SELECT e.event_id, e.event_name,
SUM(CASE WHEN r.response = 'going' THEN 1 ELSE 0 END) AS going,
SUM(CASE WHEN r.response = 'interested' THEN 1 ELSE 0 END) AS interested
FROM FbEvent e
LEFT JOIN EventRsvp r ON r.event_id = e.event_id
GROUP BY e.event_id, e.event_nameAnother way
SELECT e.event_id, e.event_name, COUNT(CASE WHEN r.response = 'going' THEN 1 END) AS going, COUNT(CASE WHEN r.response = 'interested' THEN 1 END) AS interested FROM FbEvent e LEFT JOIN EventRsvp r ON r.event_id = e.event_id GROUP BY e.event_id, e.event_nameAnother way
SELECT e.event_id, e.event_name, (SELECT COUNT(*) FROM EventRsvp r WHERE r.event_id = e.event_id AND r.response = 'going') AS going, (SELECT COUNT(*) FROM EventRsvp r WHERE r.event_id = e.event_id AND r.response = 'interested') AS interested FROM FbEvent e← Meta: Verified WhatsApp Businesses With No Live Catalog Items · Meta: Ad Accounts That Overspent Their September Limit →