Meta: Going and Interested Counts per Facebook Event

Easy
750.

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.

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

ColumnType
event_idint
event_namevarchar
starts_atdatetime

Primary key: event_id.

One row per event created on Facebook.

Table: EventRsvp

ColumnTypeValues
event_idint
user_idint
responseenumgoing, interested, declined

Primary key (together): event_id, user_id.

A user's current answer to an event's invitation — one row per (event, user).

Example

The rows may come back in any order; the columns must be the ones shown, in that order.

FbEvent

event_idevent_namestarts_at
1Bengaluru Runners Meetup2025-07-06 06:00:00
2Indie Music Night2025-07-12 19:30:00
3Startup Pitch Day2025-07-19 10:00:00
4Diwali Craft Fair2025-10-18 11:00:00

EventRsvp

event_iduser_idresponse
110going
111going
112interested
113declined
114going
210interested
214interested
315declined

Output

event_idevent_namegoinginterested
1Bengaluru Runners Meetup31
2Indie Music Night02
3Startup Pitch Day00
4Diwali Craft Fair00
Read the hints, the editorial and the reference solution
-- Write your MySQL query below

Sign in to write, run and submit queries

A free account — no card, no trial clock. You come straight back here.

Run your query to see its result next to the expected one, example by example.

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

ColumnType
event_idint
event_namevarchar
starts_atdatetime

Primary key: event_id.

One row per event created on Facebook.

Table: EventRsvp

ColumnType
event_idint
user_idint
responseenum(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_idevent_namestarts_at
1Bengaluru Runners Meetup2025-07-06 06:00:00
2Indie Music Night2025-07-12 19:30:00
3Startup Pitch Day2025-07-19 10:00:00
4Diwali Craft Fair2025-10-18 11:00:00

EventRsvp

event_iduser_idresponse
110going
111going
112interested
113declined
114going
210interested
214interested
315declined

Output

event_idevent_namegoinginterested
1Bengaluru Runners Meetup31
2Indie Music Night02
3Startup Pitch Day00
4Diwali Craft Fair00

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_name

Another 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_name

Another 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 →