Quick Overview

This question evaluates a candidate's ability to perform data aggregation, filtering, ranking, and deterministic tie-breaking on transactional datasets, assessing skills in Data Manipulation (SQL/Python).

Select Top Customers Using Transaction Data Filters

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

transactions +----+---------+------------+--------+ | id | user_id | order_date | amount | +----+---------+------------+--------+ | 1 | 101 | 2023-01-01 | 120.50 | | 2 | 102 | 2023-01-08 | 75.00 | | 3 | 101 | 2023-02-02 | 200.00 | | 4 | 103 | 2023-02-10 | 40.00 | | 5 | 104 | 2023-03-12 | 150.00 | +----+---------+------------+--------+ ##### Scenario Retail promotion targeting based on historical transaction data. ##### Question Given a transactions table, select the customers who satisfy the provided campaign filters. From those customers select exactly five with the highest total order count. Explain how you would break ties if more than five customers meet the top-5 criterion. ##### Hints Aggregate by customer, ORDER BY order_cnt DESC, LIMIT 5; add secondary sort (e.g., most recent order date or random) for deterministic tie-breaking.

Overview: This question evaluates a candidate's ability to perform data aggregation, filtering, ranking, and deterministic tie-breaking on transactional datasets, assessing skills in Data Manipulation (SQL/Python).

Using the transactions table, apply this campaign filter: orders placed between '2023-01-01' (inclusive) and '2024-01-01' (exclusive) with amount >= 100. Among the filtered transactions, aggregate by user_id to compute each customer's order count (order_cnt) and most recent order date (last_order_date). Return up to five customers with the highest order_cnt. Break ties deterministically by last_order_date descending, then user_id ascending. Return columns user_id, order_cnt, last_order_date.

Tables

transactions(id INTEGER, user_id INTEGER, order_date DATE, amount DECIMAL(10,2))

Hints

  1. Filter transactions to 2023 with amount >= 100 before aggregating.
  2. Aggregate per user_id and use ROW_NUMBER() with the specified ordering to deterministically select the top five.
AmazonData ScientistmediumData Manipulation (SQL/Python)

Top Five Customers by Orders After Campaign Filter

Using the transactions table, apply this campaign filter: orders placed between '2023-01-01' (inclusive) and '2024-01-01' (exclusive) with amount >= 100. Among the filtered transactions, aggregate by user_id to compute each customer's order count (order_cnt) and most recent order date (last_order_date). Return up to five customers with the highest order_cnt. Break ties deterministically by last_order_date descending, then user_id ascending. Return columns user_id, order_cnt, last_order_date.

Expected Output

After filtering to 2023 orders with amount >= 100, return up to five customers ordered by order_cnt DESC, last_order_date DESC, user_id ASC. On the sample data, only users 101 and 104 qualify.

Tables

transactions
transactions
ColumnType
idINTEGER
user_idINTEGER
order_dateDATE
amountDECIMAL(10,2)

Comments (1)

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions