Understand the concept
Activity is a business definition: any event, a login or a purchase. Specify which events count, whether repeats collapse to one user and which timezone defines a day.
Apply the method
The lab uses SQLite dates as ISO text. strftime formats a month, date adjusts dates and julianday supports day differences. PostgreSQL and MySQL use different functions; translate dialect-specific expressions deliberately.
Avoid a common mistake
Retention needs a cohort rule and a return rule. A buyer is not necessarily a login returner. Join on user identity and distinguish missing observation time from a genuine non-return. This synthetic dataset demonstrates logic, not population estimates.
Worked query
Return month (YYYY-MM) and revenue for paid orders, ordered by month.
SELECT strftime('%Y-%m',ordered_at) AS month,SUM(amount) AS revenue FROM orders WHERE status='paid' GROUP BY month ORDER BY month;Run and explore this example →Check your understanding
Before running your query, predict the output columns and approximate row count. After running it, inspect a missing value, a tie and a record with no match when available. Explain why the result answers the question.
Practice this topic
- Monthly revenue — Return month (YYYY-MM) and revenue for paid orders, ordered by month.
- Monthly active users — Return month (YYYY-MM) and active_users (distinct users with any event), ordered by month.
- Daily login users — Return event_date and users as distinct login users per date, ordered by date.
- Order age at month end — Return id and days_old as integer days from ordered_at to 2026-03-31, ordered by id.
- Signup cohorts — Return cohort (YYYY-MM) and signups (distinct signup users), ordered by cohort.
- Purchasing users — Return user_id and first_purchase date for purchase users, ordered by user_id.
- Next-day return — Return distinct user_id for users with a login exactly one day after signup, ordered by user_id.
- Month start labels — Return id and month_start (YYYY-MM-01), ordered by id.
- Event type mix — Return event_type and events (count), ordered by event_type.
- Paid order percentage — Return paid_percent rounded to two decimals: 100.0 times paid count / all orders.
SQLite reference: SQL language documentation. Written by Future Data Academy with AI assistance; query examples checked against the supplied teaching dataset.
