Find Third Transaction - Problem

You are analyzing user spending patterns on an e-commerce platform to identify users who show increasing spending confidence. Your task is to find the third transaction for each user where they demonstrate escalating purchase behavior.

Given a Transactions table with user spending data, you need to identify users whose third transaction amount is higher than both their first and second transactions.

Key Requirements:

  • Only consider users with at least 3 transactions
  • The third transaction must have higher spending than both preceding transactions
  • Return results ordered by user_id in ascending order

This problem helps identify users who are becoming more engaged and willing to spend more over time - valuable insights for marketing and customer retention strategies!

Table Schema:

Transactions
+------------------+----------+
| Column Name      | Type     |
+------------------+----------+
| user_id          | int      |
| spend            | decimal  |
| transaction_date | datetime |
+------------------+----------+

Input & Output

example_1.sql โ€” Basic Case
$ Input: Transactions table: | user_id | spend | transaction_date | |---------|-------|--------------------| | 1 | 65.56 | 2023-11-18 13:49:42| | 1 | 96.0 | 2023-11-30 02:47:04| | 1 | 7.44 | 2023-11-02 12:15:23| | 1 | 49.78 | 2023-11-12 00:13:46| | 2 | 40.89 | 2023-11-21 08:12:32| | 2 | 100.44| 2023-12-0901:16:05 | | 2 | 37.33 | 2023-11-11 16:22:04| | 3 | 37.0 | 2023-12-07 09:10:25|
โ€บ Output: | user_id | spend | transaction_date | |---------|-------|--------------------| | 1 | 65.56 | 2023-11-18 13:49:42|
๐Ÿ’ก Note: For user 1, transactions in chronological order: $7.44 (1st), $49.78 (2nd), $65.56 (3rd). The third transaction ($65.56) is greater than both previous ones. User 2's third transaction ($100.44) is not greater than the second ($37.33 < $100.44 is false when comparing 1st < 3rd). User 3 has only one transaction.
example_2.sql โ€” Multiple Qualifying Users
$ Input: Transactions table: | user_id | spend | transaction_date | |---------|-------|--------------------| | 1 | 10.0 | 2023-01-01 10:00:00| | 1 | 15.0 | 2023-01-02 10:00:00| | 1 | 25.0 | 2023-01-03 10:00:00| | 2 | 5.0 | 2023-01-01 11:00:00| | 2 | 12.0 | 2023-01-02 11:00:00| | 2 | 18.0 | 2023-01-03 11:00:00| | 3 | 20.0 | 2023-01-01 12:00:00| | 3 | 30.0 | 2023-01-02 12:00:00| | 3 | 25.0 | 2023-01-03 12:00:00|
โ€บ Output: | user_id | spend | transaction_date | |---------|-------|--------------------| | 1 | 25.0 | 2023-01-03 10:00:00| | 2 | 18.0 | 2023-01-03 11:00:00|
๐Ÿ’ก Note: User 1: $10 < $15 < $25 โœ“. User 2: $5 < $12 < $18 โœ“. User 3: $20 < $30 but $30 > $25, so third transaction is not greater than second โœ—.
example_3.sql โ€” Edge Cases
$ Input: Transactions table: | user_id | spend | transaction_date | |---------|-------|--------------------| | 1 | 100.0 | 2023-01-01 10:00:00| | 1 | 50.0 | 2023-01-02 10:00:00| | 1 | 75.0 | 2023-01-03 10:00:00| | 2 | 10.0 | 2023-01-01 11:00:00| | 2 | 10.0 | 2023-01-02 11:00:00| | 3 | 15.0 | 2023-01-01 12:00:00|
โ€บ Output: | user_id | spend | transaction_date | |---------|-------|-----------------| | (empty result set) |
๐Ÿ’ก Note: User 1: $75 is not greater than $100 (first transaction). User 2: Has only 2 transactions. User 3: Has only 1 transaction. No users meet the criteria.

Constraints

  • 1 โ‰ค Number of transactions โ‰ค 105
  • 1 โ‰ค user_id โ‰ค 104
  • 0.01 โ‰ค spend โ‰ค 1000.00
  • Each (user_id, transaction_date) combination is unique
  • transaction_date is in format 'YYYY-MM-DD HH:MM:SS'

Visualization

Tap to expand
Customer Spending Confidence TrackerCustomer 1 โœ“$101st$152nd$253rd25 > 15 > 10 โœ“Customer 2 โœ—$201st$302nd$253rd25 < 30 โœ—Window Function AnalysisROW_NUMBER() OVER (PARTITION BY user_id ORDER BY date)LAG() to compare with previous transactions
Understanding the Visualization
1
Organize by Customer
Group all transactions by user_id to analyze each customer separately
2
Sort by Time
Order transactions chronologically to identify the sequence of purchases
3
Number the Purchases
Assign sequence numbers (1st, 2nd, 3rd, etc.) to each transaction
4
Compare Spending
Check if the 3rd transaction amount exceeds both the 1st and 2nd
5
Select Winners
Return customers whose 3rd purchase shows increasing confidence
Key Takeaway
๐ŸŽฏ Key Insight: Window functions allow us to efficiently partition data by user and access previous row values for comparison, making this a perfect use case for LAG() and ROW_NUMBER() functions in a single query pass.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen