Meta: Facebook Groups Where Every Admin Approved a Post
The Facebook Groups team checks whether admin teams share the moderation load.
- Difficulty: Medium
- Topics: Subqueries, Joins, Dates
- Dialect: MySQL
- Problem: #758
Problem statement
The Facebook Groups team checks whether admin teams share the moderation load. A group passes for the week of 2025-03-03 to 2025-03-09 (inclusive) when every one of its current admins approved at least one post in that group during the week.
Return the passing groups with the columns group_id and group_name, in any order. Approvals by people who are not current admins of that group do not count, approvals in another group do not count, and a group with no admins does not pass.
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: FbGroup
| Column | Type |
|---|---|
| group_id | int |
| group_name | varchar |
Primary key: group_id.
One row per Facebook group.
Table: GroupAdmin
| Column | Type |
|---|---|
| group_id | int |
| user_id | int |
Primary key: group_id, user_id.
The current admins of each group.
Table: PostApproval
| Column | Type |
|---|---|
| approval_id | int |
| group_id | int |
| admin_id | int |
| approved_at | date |
Primary key: approval_id.
One row per pending post approved; admin_id is the user who approved it (possibly a former admin).
Examples
Example 1
FbGroup
| group_id | group_name |
|---|---|
| 1 | Bengaluru Foodies |
| 2 | Pune Cyclists |
| 3 | Kochi Book Club |
| 4 | Delhi Startup Founders |
GroupAdmin
| group_id | user_id |
|---|---|
| 1 | 10 |
| 1 | 11 |
| 2 | 12 |
| 2 | 13 |
| 3 | 14 |
PostApproval
| approval_id | group_id | admin_id | approved_at |
|---|---|---|---|
| 1 | 1 | 10 | 2025-03-03 |
| 2 | 1 | 11 | 2025-03-09 |
| 3 | 2 | 12 | 2025-03-05 |
| 4 | 2 | 13 | 2025-03-10 |
| 5 | 3 | 14 | 2025-03-04 |
| 6 | 3 | 15 | 2025-03-04 |
| 7 | 1 | 10 | 2025-03-06 |
| 8 | 2 | 13 | 2025-03-02 |
Output
| group_id | group_name |
|---|---|
| 1 | Bengaluru Foodies |
| 3 | Kochi Book Club |
How to solve Meta: Facebook Groups Where Every Admin Approved a Post
This is relational division: keep a group only if all of its admins have a matching approval. SQL has no "for all", so the solution uses the double negative — there is no admin of the group for whom there is no approval. The innermost NOT EXISTS asks whether one admin approved something in that group between 3 and 9 March; the outer NOT EXISTS requires that no admin fails that test.
A universal statement over an empty set is true, so a group without admins would pass the double negative. The statement says it must not, hence the extra EXISTS on GroupAdmin. Approvals by former admins never match a GroupAdmin row, and an approval in another group fails the p.group_id = a.group_id test, so neither helps.
The counting alternative joins every admin to their distinct approvals in the week (a LEFT JOIN, so admins without one keep a NULL row) and keeps groups where the number of admins equals the number of matched admins. The inner join to GroupAdmin drops admin-less groups by itself. Both are linear with indexes on the join keys.
Reference solution (MySQL)
SELECT g.group_id, g.group_name
FROM FbGroup g
WHERE EXISTS (SELECT 1 FROM GroupAdmin a WHERE a.group_id = g.group_id)
AND NOT EXISTS (
SELECT 1 FROM GroupAdmin a
WHERE a.group_id = g.group_id
AND NOT EXISTS (
SELECT 1 FROM PostApproval p
WHERE p.group_id = a.group_id AND p.admin_id = a.user_id
AND p.approved_at BETWEEN '2025-03-03' AND '2025-03-09'))Another way
SELECT g.group_id, g.group_name
FROM FbGroup g
JOIN GroupAdmin a ON a.group_id = g.group_id
LEFT JOIN (SELECT DISTINCT group_id, admin_id FROM PostApproval
WHERE approved_at BETWEEN '2025-03-03' AND '2025-03-09') p
ON p.group_id = a.group_id AND p.admin_id = a.user_id
GROUP BY g.group_id, g.group_name
HAVING COUNT(*) = COUNT(p.admin_id)← Meta: Last-Click Attributed Revenue by Campaign · Meta: WhatsApp Business Message Delivery Funnel →