Meta-style Company ChallengeHardVerified answerSQLite live

User Influence Score Dashboard

For each active user, calculate a composite influence score based on posts, average engagement, friend network, and peak engagement — then rank them.

  • CTEs
  • Window functions
  • Joins
  • Subqueries
  • Aggregation

Challenge brief

Understand the request

Creator Monetisation needs a composite influence score for every active user to determine which creators qualify for the next monetisation cohort.

Build a multi-factor influence score using three CTEs combining post metrics, engagement averages, and friend counts.

Return

  • username
  • total_posts
  • avg_engagement_per_post
  • friend_count
  • influence_score (posts*10 + avg_engagement*2 + friends*15 + max_engagement*0.5)
  • influence_rank (RANK desc)

Constraints

  • Return active users only
  • Treat missing post or friend metrics as zero
  • Users with equal influence scores share a rank

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

users

  • user_idINTEGER
  • usernameVARCHAR(50)
  • statusVARCHAR(20)

posts

  • post_idINTEGER
  • user_idINTEGER
  • likes_countINTEGER
  • comments_countINTEGER
  • shares_countINTEGER

friendships

  • user_id_1INTEGER
  • user_id_2INTEGER
  • statusVARCHAR(20)

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Three CTEs: (1) user_posts: post count, avg engagement, max engagement per user. (2) user_friends: UNION ALL bidirectional friendships, count per user. (3) influence_calc: COALESCE all metrics to 0, apply the scoring formula. Then RANK() OVER (ORDER BY influence_score DESC).

Hint 2

user_posts: GROUP BY user, AVG(likes+comments+shares), MAX, COUNT. user_friends: UNION ALL + COUNT. influence_calc: posts*10 + avg*2 + friends*15 + max*0.5. WHERE u.status = 'active'.

Hint 3

Start with the tables that establish the result grain for question 14, select the required output aliases, and add the remaining joins, filters, aggregation, and ordering one clause at a time.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
WITH user_posts AS (SELECT p.user_id, COUNT(p.post_id) AS total_posts, ROUND(AVG(p.likes_count + p.comments_count + p.shares_count), 2) AS avg_engagement_per_post, MAX(p.likes_count + p.comments_count + p.shares_count) AS max_engagement FROM posts p GROUP BY p.user_id), user_friends AS (SELECT user_id_1 AS user_id, user_id_2 AS friend_id FROM friendships WHERE status = 'accepted' UNION ALL SELECT user_id_2 AS user_id, user_id_1 AS friend_id FROM friendships WHERE status = 'accepted'), friend_counts AS (SELECT user_id, COUNT(friend_id) AS friend_count FROM user_friends GROUP BY user_id), influence_calc AS (SELECT u.user_id, u.username, COALESCE(up.total_posts, 0) AS total_posts, COALESCE(up.avg_engagement_per_post, 0) AS avg_engagement_per_post, COALESCE(fc.friend_count, 0) AS friend_count, ROUND((COALESCE(up.total_posts, 0) * 10 + COALESCE(up.avg_engagement_per_post, 0) * 2 + COALESCE(fc.friend_count, 0) * 15 + COALESCE(up.max_engagement, 0) * 0.5), 2) AS influence_score FROM users u LEFT JOIN user_posts up ON u.user_id = up.user_id LEFT JOIN friend_counts fc ON u.user_id = fc.user_id WHERE u.status = 'active') SELECT username, total_posts, avg_engagement_per_post, friend_count, influence_score, RANK() OVER (ORDER BY influence_score DESC) AS influence_rank FROM influence_calc ORDER BY influence_rank, username;

Why this works

COALESCE handles users with no posts (their avg/max would be NULL). The formula rewards engagement quality (avg*2) and network size (friends*15) more than raw post count (posts*10). alex_brown is excluded because status = 'inactive'.

Success check

4 active users — jane_smith leads (619.5), john_doe ranks last (232)

Expected result

Use this output to verify values, aliases, ordering, and row count.

usernametotal_postsavg_engagement_per_postfriend_countinfluence_scoreinfluence_rank
jane_smith22142619.51
sarah_jones120625552
mike_wilson11292362.53
john_doe27022324

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Interview Practice

Return to the complete interview preparation experience.