Table Schema
Inspect
Table
| Column | Type |
|---|---|
| post_id | integer |
| content_type | varchar |
| Column | Type |
|---|---|
| post_id | integer |
Find the most popular content type based on reactions.
Table Schema
| Column | Type |
|---|---|
| post_id | integer |
| content_type | varchar |
| Column | Type |
|---|---|
| post_id | integer |
Sample Data
| post_id | content_type |
|---|---|
| 1 | video |
| 2 | image |
| 3 | video |
| 4 | text |
| post_id |
|---|
| 1 |
| 1 |
| 2 |
| 3 |
| 3 |
| 3 |
| 4 |
| content_type | total_reactions |
|---|---|
| video | 5 |
SQL Editor
| content_type | total_reactions |
|---|---|
| video | 5 |
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.content_type, COUNT(*) AS total_reactions FROM reactions r JOIN posts p ON r.post_id = p.post_id GROUP BY p.content_type ORDER BY total_reactions DESC LIMIT 1;
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