June purchases compared with the first purchase
The task
The lifecycle team wants to see how a customer's big June orders compare with the very first order they ever placed.
Look at purchases made from 2025-06-01 up to, but not including, 2025-07-01 with an amount of at least 80. For each one return:
purchase_iduser_idamountfirst_amount: theamountof that user's first purchase of all time, whatever its size and date. The first purchase is the one with the earliestpurchased_at. If a user has two purchases with the samepurchased_at, the one with the lowerpurchase_idis first.vs_first:amountminusfirst_amount
A user's first purchase can itself be one of the June rows. Sort by purchase_id.
Expected output
purchase_iduser_idamountfirst_amountvs_first- Sorted by purchase_id ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Marketing team · click a name to insert it
campaigns10 rows in the sample
One row per campaign. Key: campaign_id.
| int | Unique id. | |
| text | Name. | |
| text | search, social, video, email or affiliate. | |
| date | First day live. | |
| date | Last day live. NULL if still running. | |
| numeric(10,2) | Budget per day, USD. |
ad_daily969 rows in the sample
One row per campaign per day it served ads. Days with no delivery have no row. Key: campaign_id, day.
| int | The campaign. | |
| date | The day. | |
| int | Times an ad was shown. | |
| int | Clicks. | |
| numeric(10,2) | Money spent, USD. |
signups2,108 rows in the sample
One row per new user. campaign_id is the campaign credited with the sign-up. Key: user_id.
| int | Unique id. | |
| timestamp | When they signed up (UTC). | |
| int | Credited campaign. NULL for organic sign-ups. | |
| text | Two-letter country code. |
purchases2,294 rows in the sample
One row per paid order by a user. Key: purchase_id.
| int | Unique id. | |
| int | Buyer. | |
| timestamp | When (UTC). | |
| numeric(10,2) | Order value, USD. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL