sqlstreak

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

weekactive_users
2025-12-293
2026-01-0516
2026-01-1230
2026-01-1939
2026-01-2648
2026-02-0267
2026-02-0974

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

countryuserssubscribersconversion_pct
IN522446.2
DE321443.8
BR461532.6
GB451431.1
US1253326.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

monthnew_subschange
2026-0118NULL
2026-023315
2026-03418
2026-048-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

deviceplaysavg_minutesshare_pct
mobile144947.160.2
desktop73347.430.5
tv22541.59.3

Try it: PostgreSQL playground · SQLite playground

The lessons that teach this

  1. 01GROUP BYCounting things · Pro
  2. 02LEFT JOINJoining tables · Pro
  3. 03WITH: name your stepsQueries inside queries · Pro
  4. 04Running totalsWindow functions · Pro
  5. 05Working with datesText and dates · Pro
  6. 06Active users and growthAnalytics patterns · Pro
  7. 07Funnels and conversionAnalytics patterns · Pro
  8. 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.

Start free