SQL
Lab
Meta Meta Interview Question 01

Post
Engagement
Rate

Calculate engagement count per post.

Table Schema

Inspect
Table

interactive
ColumnType
post_idinteger
user_idinteger
created_atdate
ColumnType
reaction_idinteger
post_idinteger
reaction_typevarchar

Sample Data

Input
Output

Sample Input: posts
post_iduser_idcreated_at
11012024-01-01
21022024-01-02
31032024-01-03
Sample Input: reactions
reaction_idpost_idreaction_type
11like
21comment
31share
42like
52like
63comment
73share
Expected Output
post_idengagement_count
13
22
32

SQL Editor

Run
Query

postgresql
Waiting for query

post_idengagement_count
13
22
32

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.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

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