Airbnb-style Company ChallengeMediumVerified answerSQLite live

Guest Total Spend

Return user_id, country, and total_spent for each guest with paid bookings. Order by total_spent descending, then user_id.

  • Joins
  • Aggregation
  • Sorting

Challenge brief

Understand the request

Finance — Guest Lifetime Value Finance uses total guest spend to calculate lifetime value (LTV) and decide eligibility for loyalty rewards.

Calculate how much each guest has spent in total on paid bookings.

Return

  • user_id
  • country
  • total_spent

Constraints

  • Include guests with matched booking payments
  • Sum every payment transaction at guest grain
  • Order by spend descending, then user ID

Data you will use

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

bookings

  • booking_idINTEGER
  • user_idINTEGER

users

  • user_idINTEGER
  • countryVARCHAR(50)

payments

  • booking_idINTEGER
  • amountINTEGER

Hints, when you need them

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

Hint 1

A payment reaches its guest through the related booking.

Hint 2

Aggregate payment amounts at user and country grain.

Hint 3

Use user ID to resolve equal spend.

Verified SQL answer

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

Reveal solution and explanation
SELECT b.user_id, u.country, SUM(p.amount) AS total_spent FROM bookings b JOIN users u ON b.user_id = u.user_id JOIN payments p ON b.booking_id = p.booking_id GROUP BY b.user_id, u.country ORDER BY total_spent DESC, b.user_id

Why this works

country comes from users; payment amount from payments. Both join via bookings as the bridge. SUM per user gives total lifetime spend from paid bookings.

Success check

Returns one stable row per paying guest with complete transaction spend

Expected result

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

user_idcountrytotal_spent
8DE640
6FR600
5US560
2IN500
4CA360
3UK300
7US300
1US200

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.