SQL for data analysts: the queries the job is made of
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Analyst work in SQL comes down to a handful of shapes: count active users, follow people through a funnel, compare periods, split by segment. Below, each one runs on Streamly, a small music streaming app's data (users, plays, subscriptions), followed by the lessons that teach them step by step.
Active users per week
COUNT(DISTINCT user_id) counts people, not plays. Truncate the date to the week to group.
PostgreSQL
SELECT date_trunc('week', played_on)::date AS week,
COUNT(DISTINCT user_id) AS active_users
FROM plays
WHERE played_on < '2026-02-16'
GROUP BY 1
ORDER BY 1;SQLite
SELECT date(played_on, '-6 days', 'weekday 1') AS week,
COUNT(DISTINCT user_id) AS active_users
FROM plays
WHERE played_on < '2026-02-16'
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 7 rows
| week | active_users |
|---|---|
| 2025-12-29 | 3 |
| 2026-01-05 | 16 |
| 2026-01-12 | 30 |
| 2026-01-19 | 39 |
| 2026-01-26 | 48 |
| 2026-02-02 | 67 |
| 2026-02-09 | 74 |
Try it: PostgreSQL playground · SQLite playground
Conversion by segment
Every user, a LEFT JOIN to subscriptions, and a share. LEFT JOIN keeps the users who never subscribed.
SELECT u.country,
COUNT(*) AS users,
COUNT(s.user_id) AS subscribers,
ROUND(100.0 * COUNT(s.user_id) / COUNT(*), 1) AS conversion_pct
FROM users u
LEFT JOIN subscriptions s ON s.user_id = u.id
GROUP BY u.country
HAVING COUNT(*) >= 30
ORDER BY conversion_pct DESC;Result (PostgreSQL and SQLite, identical): 5 rows
| country | users | subscribers | conversion_pct |
|---|---|---|---|
| IN | 52 | 24 | 46.2 |
| DE | 32 | 14 | 43.8 |
| BR | 46 | 15 | 32.6 |
| GB | 45 | 14 | 31.1 |
| US | 125 | 33 | 26.4 |
Try it: PostgreSQL playground · SQLite playground
Period over period
Monthly totals in a CTE, then LAG puts last month beside this month.
WITH monthly AS (
SELECT SUBSTR(CAST(started_on AS TEXT), 1, 7) AS month, COUNT(*) AS new_subs
FROM subscriptions
GROUP BY 1
)
SELECT month, new_subs,
new_subs - LAG(new_subs) OVER (ORDER BY month) AS change
FROM monthly
ORDER BY month;Result (PostgreSQL and SQLite, identical): 4 rows
| month | new_subs | change |
|---|---|---|
| 2026-01 | 18 | NULL |
| 2026-02 | 33 | 15 |
| 2026-03 | 41 | 8 |
| 2026-04 | 8 | -33 |
Try it: PostgreSQL playground · SQLite playground
Engagement by device
Averages and shares in one query: minutes per play and each device's share of all plays.
SELECT device,
COUNT(*) AS plays,
ROUND(AVG(minutes), 1) AS avg_minutes,
ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS share_pct
FROM plays
GROUP BY device
ORDER BY plays DESC;Result (PostgreSQL and SQLite, identical): 3 rows
| device | plays | avg_minutes | share_pct |
|---|---|---|---|
| mobile | 1449 | 47.1 | 60.2 |
| desktop | 733 | 47.4 | 30.5 |
| tv | 225 | 41.5 | 9.3 |
Try it: PostgreSQL playground · SQLite playground
The lessons that teach this
- 01GROUP BYCounting things · Pro
- 02LEFT JOINJoining tables · Pro
- 03WITH: name your stepsQueries inside queries · Pro
- 04Running totalsWindow functions · Pro
- 05Working with datesText and dates · Pro
- 06Active users and growthAnalytics patterns · Pro
- 07Funnels and conversionAnalytics patterns · Pro
- 08Cohorts and retentionAnalytics patterns · Pro
Keep going
Write your first query in about a minute
Five-minute lessons on a real database, with instant feedback. The first units are free, no account needed.