cswork / libraryOpen practice workspace

Coding questions · Uber

Calculate January-2024 SF Promotion Impact Using SQL Queries

This question evaluates a candidate's proficiency in relational data manipulation techniques—specifically joins, date and region filtering, aggregation, top‑N identification, and percentage calculations applied to experiment and user-level metrics.

campaign_users

+---------+-----------+
| user_id | treatment |
+---------+-----------+
| 1001    | control   |
| 1002    | test      |
| 1003    | control   |
| 1004    | test      |
| 1005    | control   |
+---------+-----------+

sessions

+---------+-----------+--------+---------------------+
| user_id | session_id| region | timestamp           |
+---------+-----------+--------+---------------------+
| 1001    | s1        | SF     | 2024-01-05 10:00:00 |
| 1002    | s2        | NY     | 2024-01-06 11:00:00 |
| 1002    | s3        | SF     | 2024-01-10 12:00:00 |
| 1004    | s4        | SF     | 2024-01-12 09:30:00 |
| 1005    | s5        | SF     | 2024-02-01 08:20:00 |
+---------+-----------+--------+---------------------+

orders

+---------+-----------+----------+---------+--------+
| user_id | session_id| order_id | revenue | profit |
+---------+-----------+----------+---------+--------+
| 1001    | s1        | o101     | 50.00   | 10.00  |
| 1002    | s2        | o102     | 80.00   | 15.00  |
| 1002    | s3        | o103     | 45.00   | 12.00  |
| 1004    | s4        | o104     | 60.00   | 20.00  |
| 1004    | s4        | o105     | 20.00   | 5.00   |
+---------+-----------+----------+---------+--------+
Scenario

Using three relational tables that record campaign assignment, user sessions, and orders, evaluate the January-2024 SF-only promotion experiment and perform follow-up analyses.

Question

Write a SQL query that joins the three tables to compute average revenue per user for treatment vs. control, restricting data to sessions in January-2024 and region = 'SF'. For each treatment arm, return the three user_id values with the highest number of orders. For each treatment arm, calculate the percentage of users who have placed more than five orders.

Hints

Use proper JOINs, WHERE filters on date and region, GROUP BY treatment, COUNT orders, window functions or conditional aggregation as needed.

Campaign experiment: SF January-2024 revenue and ordering behavior

You are analyzing the impact of a January-2024 SF-only promotion experiment. Using the three tables below (campaign assignment, user sessions, and orders), write a SQL query that:

  1. Restricts data to sessions in region = 'SF' with timestamps between '2024-01-01' (inclusive) and '2024-02-01' (exclusive).
  2. Joins the three tables to compute the average revenue per user for each treatment arm (control vs test), based only on qualifying sessions.
  3. For each treatment arm, returns the three user_id values with the highest number of orders (within the filtered SF January sessions).
  4. For each treatment arm, calculates the percentage of users who have placed more than five orders (again, considering only orders from qualifying sessions). Return one row per treatment arm including: treatment, average revenue per user, the top three user_ids by order count, and the percentage of users with more than five orders.

Table campaign_users

columntypeconstraints
user_idINTEGERPRIMARY KEY, NOT NULL
treatmentVARCHAR(10)NOT NULL

Sample data:

treatmentuser_id
control1001
test1002
control1003
test1004
control1005

Table sessions

columntypeconstraints
user_idINTEGERNOT NULL, FOREIGN KEY REFERENCES campaign_users(user_id)
session_idVARCHAR(20)PRIMARY KEY, NOT NULL
regionVARCHAR(10)NOT NULL
timestampTIMESTAMPNOT NULL

Sample data:

regionsession_idtimestampuser_id
SFs12024-01-05 10:00:001001
NYs22024-01-06 11:00:001002
SFs32024-01-10 12:00:001002
SFs42024-01-12 09:30:001004
SFs52024-02-01 08:20:001005

Table orders

columntypeconstraints
user_idINTEGERNOT NULL, FOREIGN KEY REFERENCES campaign_users(user_id)
session_idVARCHAR(20)NOT NULL, FOREIGN KEY REFERENCES sessions(session_id)
order_idVARCHAR(20)PRIMARY KEY, NOT NULL
revenueDECIMAL(10,2)NOT NULL
profitDECIMAL(10,2)NOT NULL

Sample data:

order_idprofitrevenuesession_iduser_id
o1011050s11001
o1021580s21002
o1031245s31002
o1042060s41004
o105520s41004

Expected output

One row per treatment arm, restricted to sessions with region = 'SF' and timestamp between '2024-01-01' (inclusive) and '2024-02-01' (exclusive). For each arm: (1) average revenue per user based on qualifying sessions, (2) an array of up to the top three user_ids ordered by descending number of orders (and by revenue and user_id as tie-breakers if needed), and (3) the percentage of users in that arm who have more than five orders from qualifying sessions.

avg_revenue_per_userpct_users_gt_5_orderstop_user_ids_by_orderstreatment
5001001control
62.501004,1002test

Hints

  1. Filter sessions to region = 'SF' and timestamp >= '2024-01-01' and < '2024-02-01'.
  2. Join campaign_users to the filtered sessions, then LEFT JOIN orders to keep users with zero orders on qualifying sessions.
  3. Aggregate to a user-level table (order_count, user_revenue) before computing treatment-level averages.
  4. Use window functions to rank users by order_count within each treatment arm and aggregate the top three (e.g., ARRAY_AGG or STRING_AGG).
  5. Compute the percentage of heavy-order users with conditional aggregation, e.g. AVG(CASE WHEN order_count > 5 THEN 1.0 ELSE 0.0 END) * 100.