Table Schema
Inspect
Table
| Column | Type |
|---|---|
| post_id | integer |
| user_id | integer |
| created_at | date |
| Column | Type |
|---|---|
| reaction_id | integer |
| post_id | integer |
| reaction_type | varchar |
Calculate engagement count per post.
Table Schema
| Column | Type |
|---|---|
| post_id | integer |
| user_id | integer |
| created_at | date |
| Column | Type |
|---|---|
| reaction_id | integer |
| post_id | integer |
| reaction_type | varchar |
Sample Data
| post_id | user_id | created_at |
|---|---|---|
| 1 | 101 | 2024-01-01 |
| 2 | 102 | 2024-01-02 |
| 3 | 103 | 2024-01-03 |
| reaction_id | post_id | reaction_type |
|---|---|---|
| 1 | 1 | like |
| 2 | 1 | comment |
| 3 | 1 | share |
| 4 | 2 | like |
| 5 | 2 | like |
| 6 | 3 | comment |
| 7 | 3 | share |
| post_id | engagement_count |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
SQL Editor
| post_id | engagement_count |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT p.post_id, COUNT(r.reaction_id) AS engagement_count FROM posts p LEFT JOIN reactions r ON p.post_id = r.post_id GROUP BY p.post_id;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions