Career Hub/Interview questions/Average Shares Per Post
Meta logo

Average Shares Per Post

Meta·easy·SQL

You are given a table of posts and a table of post shares. Find the average number of shares per post for each user. Include users who have posts but received no shares, showing 0 for them. Return user_id and avg_shares_per_post rounded to 2 decimal places, ordered by user_id.

Schema

user_posts
post_idINTEGER
user_idINTEGER
contentVARCHAR
post_dateDATE
post_shares
share_idINTEGER
post_idINTEGER
shared_by_user_idINTEGER
shared_atTIMESTAMP

Target output

Target output
user_idavg_shares_per_post
1011.50
1020.00
1030.00
1042.00
1050.00

What this question tests

Tests join direction and NULL arithmetic: keeping users whose posts earned no shares requires a LEFT JOIN and a zero fallback, and the denominator must be posts, not shares. That denominator is the trap most candidates hit.

More SQL interview questions

  • Currently Active Pages · Meta · medium
  • Platform Split · Netflix · medium
  • Viewership Drop Alert · Netflix · hard
Queryuser_posts, post_shares
Cmd+Enterto runLoading...
Output

Loading sample data...