Google-style Company ChallengeMediumVerified answerSQLite live

Revenue by User

What is the total ad revenue each user has generated through ad clicks?

  • Aggregation
  • Numeric functions
  • Sorting

Challenge brief

Understand the request

Ads Revenue is building a user-level revenue attribution model and needs the total ad click revenue generated per user.

Return user_id, total_revenue in the declared deterministic order.

Return

  • user_id
  • total_revenue

Constraints

  • Return users with at least one ad click
  • Round revenue to two decimals
  • Order by revenue descending, then user ID

Data you will use

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

ad_clicks

  • click_idINTEGER
  • user_idINTEGER
  • ad_idINTEGER
  • click_dateDATE
  • revenueREAL

Hints, when you need them

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

Hint 1

Revenue is recorded on ad-click facts.

Hint 2

Sum revenue independently for each user and round for presentation.

Hint 3

Order by the metric and use user ID for ties.

Verified SQL answer

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

Reveal solution and explanation
SELECT user_id, ROUND(SUM(revenue), 2) AS total_revenue FROM ad_clicks GROUP BY user_id ORDER BY total_revenue DESC, user_id

Why this works

20 users have at least one ad click. Users 21, 22, 23 have no ad_clicks rows and do not appear. User 2 earned $11.25 across 3 clicks. User 1 earned exactly $10 across 4 clicks (each worth $2.50).

Success check

Returns the complete deterministic result for revenue by user

Expected result

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

user_idtotal_revenue
211.25
110
410
58.4
77
106.4
165
184.6
94.5
134.1

Previewing 10 of 21 expected rows. Run the query in the editor to inspect the full result.

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.