Average reactions per post type

joins

Average reactions per post type

Meta SQL Interview Question

Meta's feed ranking team wants to know which kinds of posts get the most engagement. Every reaction, such as a like or a love, is recorded against the post it was left on.

Find the average number of reactions per post for each post_type, as avg_reactions, rounded to 2 decimal places. Posts that received no reactions still count toward their type's average.

Asked of

  • Data Analyst
  • Product Analyst
  • Data Scientist
  • Analytics Engineer

postsTable12 rows

Column NameType
post_idBIGINT
user_idBIGINT
post_typeVARCHAR
created_dateDATE

reactionsTable18 rows

Column NameType
reaction_idBIGINT
post_idBIGINT
reactor_idBIGINT
reaction_typeVARCHAR
reacted_dateDATE

postsExample Input

post_iduser_idpost_typecreated_date
121photo2024-09-01
222video2024-09-01
321reel2024-09-02
423text2024-09-02
524photo2024-09-03
622photo2024-09-04
723video2024-09-04
824reel2024-09-06

reactionsExample Input

reaction_idpost_idreactor_idreaction_typereacted_date
1131like2024-09-01
2132love2024-09-01
3133like2024-09-02
4231like2024-09-01
5334haha2024-09-02
6335like2024-09-02
7331love2024-09-03
8332like2024-09-03
9533like2024-09-03
10636like2024-09-04
11731love2024-09-04
12734like2024-09-05
13835like2024-09-06
14836haha2024-09-06
15832like2024-09-07

Example Output

post_typeavg_reactions
photo1.67
reel3.5
text0
video1.5

Explanation

The three photo posts received 3, 1 and 1 reactions, which is 5 reactions across 3 posts, or 1.67. The only text post received no reactions at all, so text averages 0 instead of being left out.

The example above is a small slice of the data. Your query runs against the full tables.