cswork / libraryOpen practice workspace

Coding questions · Yahoo

Calculate Total Spend and Identify Key User Metrics

This question evaluates a data scientist's proficiency in transactional data manipulation using SQL and Python, focusing on aggregation, ranking, joins, date arithmetic, and numeric rounding to produce user-level and time-series metrics.

Transactions

+----+---------+--------+------------+
| id | user_id | amount |    date    |
+----+---------+--------+------------+
| 1  |   101   | 23.50  | 2023-07-01 |
| 2  |   102   | 12.99  | 2023-07-01 |
| 3  |   101   | 50.00  | 2023-07-02 |
| 4  |   103   | 99.95  | 2023-07-02 |
| 5  |   101   |  9.99  | 2023-07-03 |
+----+---------+--------+------------+
Scenario

You have one table that stores all monetary transactions made by users of an e-commerce site.

Question

For every user, return total_spend rounded to two decimals. 2. Return the user_id(s) that have the second-highest total spend. 3. List users whose transaction_count exceeds the average transaction_count of all users. 4. For each calendar date, calculate revenue_drop_pct compared with the previous day and keep only dates where the drop is ≥10%. 5. For every user, return first_purchase_date together with amount spent on that first day.

Hints

Window functions, aggregation, self-joins, DATE arithmetic and ROUND will be useful.

Total spend per user

For every user, return their total_spend rounded to two decimals.

Table Transactions

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
dateDATENOT NULL

Sample data:

amountdateiduser_id
23.52023-07-011101
12.992023-07-012102
502023-07-023101
99.952023-07-024103
9.992023-07-035101

Expected output

Total spend per user computed from all their transactions, rounded to two decimals.

total_spenduser_id
83.49101
12.99102
99.95103

Hints

  1. Aggregate by user_id
  2. Use ROUND to two decimals

Second-highest total spenders

Return the user_id(s) that have the second-highest total spend across all transactions.

Table Transactions

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
dateDATENOT NULL

Sample data:

amountdateiduser_id
23.52023-07-011101
12.992023-07-012102
502023-07-023101
99.952023-07-024103
9.992023-07-035101

Expected output

User IDs whose total spend ranks second when ordering totals descending. Includes all ties for second place if any.

user_id
101

Hints

  1. First aggregate spend per user
  2. Use DENSE_RANK over total_spend DESC

Above-average transaction counts

List users whose transaction_count exceeds the average transaction_count computed across all users.

Table Transactions

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
dateDATENOT NULL

Sample data:

amountdateiduser_id
23.52023-07-011101
12.992023-07-012102
502023-07-023101
99.952023-07-024103
9.992023-07-035101

Expected output

Users whose number of transactions is strictly greater than the average number of transactions per user.

transaction_countuser_id
3101

Hints

  1. Count transactions per user first
  2. Compare to the average of those counts

Daily revenue drop percent

For each calendar date, compute revenue_drop_pct compared with the previous day and return only dates where the drop is at least 10%.

Table Transactions

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
dateDATENOT NULL

Sample data:

amountdateiduser_id
23.52023-07-011101
12.992023-07-012102
502023-07-023101
99.952023-07-024103
9.992023-07-035101

Expected output

Dates where day-over-day revenue decreased by at least 10%, with the percentage drop rounded to two decimals.

daterevenue_drop_pct
2023-07-0393.34

Hints

  1. Aggregate daily revenue first
  2. Use LAG to access previous day
  3. Filter drops >= 10%

First purchase date spend

For every user, return the first_purchase_date and the total amount spent on that first day.

Table Transactions

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
dateDATENOT NULL

Sample data:

amountdateiduser_id
23.52023-07-011101
12.992023-07-012102
502023-07-023101
99.952023-07-024103
9.992023-07-035101

Expected output

Each user's first transaction date along with the total amount they spent on that first day.

amount_on_first_dayfirst_purchase_dateuser_id
23.52023-07-01101
12.992023-07-01102
99.952023-07-02103

Hints

  1. Find MIN(date) per user
  2. Join back to sum amounts on that date