SQL
Lab
Meta Meta Interview Question 05

Most
Engaged
User

Find the user who generated the highest total engagement.

Table Schema

Inspect
Table

interactive
ColumnType
post_idinteger
user_idinteger
ColumnType
post_idinteger
reaction_idinteger

Sample Data

Input
Output

Sample Input: posts
post_iduser_id
1101
2102
3101
Sample Input: reactions
post_idreaction_id
11
12
23
34
35
36
Expected Output
user_idtotal_engagement
1015

SQL Editor

Run
Query

postgresql
Waiting for query

user_idtotal_engagement
1015

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT p.user_id, COUNT(r.reaction_id) AS total_engagement
FROM posts p
JOIN reactions r ON p.post_id = r.post_id
GROUP BY p.user_id
ORDER BY total_engagement DESC
LIMIT 1;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving