KatInterview

Highest Spender in the Company's First Decade

SQL & DataModel answer
EasyModel answerSQL & Datasqlaggregationwindow-functions

Last updated

Identify which user spent the most money during the initial ten-year period starting from the company's very first transaction.

You have two tables at your disposal: transactions and users.

The transactions table schema:

  • transaction_id (integer)
  • user_id (integer)
  • amount (float)
  • transaction_date (date)

The users table schema:

  • user_id (integer)
  • signup_date (date)

Your task is to determine which user accumulated the largest total spending during the company's first decade of operation. The company's inception date is defined as the earliest transaction date found in the transactions table. When multiple users are tied with identical top spending amounts, your query should return all of them.

Example:

Given these transactions:

transaction_iduser_idamounttransaction_date
11015002010-01-05
21028002010-06-01
31014002015-02-01
410320002022-01-01

Expected output:

user_idtotal_spent
101900

The company began on 2010-01-05, so the ten-year window closes on 2020-01-05. Transaction 4 falls outside this range and is excluded. User 101's total is 900 (500 + 400), which exceeds user 102's 800.

Hints

Hint 1

First determine the company's founding date by finding the minimum transaction date across all records.

Hint 2

After establishing the time window boundaries, filter transactions to include only those within the first ten years, then aggregate spending per user.

Hint 3

When selecting the top spender, use RANK() rather than LIMIT 1 to ensure all tied users are included in your result.