ProSkillsLibraryFreePassScan
Data & SQL

Data & SQL · agent skill

Analytics SQL

Readable analytics queries you can trust.

What it does

Loads when it is needed

Your agent loads it when the user writes a report, metric or analytics query. It carries 5 rules and reads a profile.md with your details before it starts, so the output matches your brand and project.

Same prompt, with and without

Week-1 retention by cohort

Prompt · Give me week-1 retention by signup week for the last 8 complete cohorts, for the growth dashboard. Active means any event.

users(id bigint PK, created_at timestamptz not null, plan text)
events(id bigint PK, user_id bigint NULL, name text, occurred_at timestamptz not null)  -- ~40M rows
indexes: events(occurred_at); none on events(user_id)
Reporting: UTC, weeks start Monday. Dashboard reads the result as-is.

Profile used: Postgres 16, 40M-row events table, no ORM (dbt models); snake_case, _at timestamps, UTC

Without the skill
I'm defining week-1 retention as: of users who signed up in week N (Monday start, UTC), the share active (any event) during week N+1. A cohort is only "complete" once week N+1 has ended, so the last 8 complete cohorts are the 8 weeks ending two Mondays ago. with bounds as ( select date_trunc('week', now() at time zone 'utc') as this_week ), cohort as ( select u.id as user_id, date_trunc('week', u.created_at at time zone 'utc') as cohort_week from users u, bounds b where u.created_at >= (b.this_week - interval '9 weeks') at time zone 'utc' and u.created_at < (b.this_week - interval '1 week') at time zone 'utc' ), active as ( select distinct e.user_id, date_trunc('week', e.occurred_at at time zone 'utc') as active_week from events e, bounds b where e.occurred_at >= (b.this_week - interval '8 weeks') at time zone 'utc' and e.occurred_at < b.this_week at time zone 'utc' and e.user_id is not null ) select c.cohort_week::date as cohort_week, count(*) as cohort_size, count(a.user_id) as retained_users, round(100.0 * count(a.user_id) / count(*), 1) as week_1_retention_pct from cohort c left join active a on a.user_id = c.user_id and a.active_week = c.cohort_week + interval '1 week' group by 1 order by 1; …
With the skill
Query: week-1 retention by signup cohort (Postgres) Question: share of users who signed up in week W (UTC, Monday start) with any event in week W+1; last 8 complete cohorts. WITH params AS ( -- grain: one row; half-open UTC window SELECT (date_trunc('week', now() AT TIME ZONE 'UTC') - interval '8 weeks') AT TIME ZONE 'UTC' AS c_start, date_trunc('week', now() AT TIME ZONE 'UTC') AT TIME ZONE 'UTC' AS c_end, date_trunc('week', now() AT TIME ZONE 'UTC') AS cur_week), cohorts AS ( -- grain: one row per user (users.id is PK) SELECT u.id AS user_id, date_trunc('week', u.created_at AT TIME ZONE 'UTC') AS cohort_week FROM users u, params p WHERE u.created_at >= p.c_start AND u.created_at < p.c_end), activity AS ( -- grain: one row per (user_id, active_week), complete weeks only SELECT DISTINCT e.user_id, date_trunc('week', e.occurred_at AT TIME ZONE 'UTC') AS active_week FROM events e, params p WHERE e.occurred_at >= p.c_start AND e.occurred_at < p.c_end AND e.user_id IS NOT NULL), retained AS ( -- grain: one row per cohort_week; join is 1 row per user (activity is distinct) SELECT c.cohort_week, COUNT(*) AS users FROM cohorts c JOIN activity a ON a.user_id = c.user_id AND a.active_week = c.cohort_week + interval '1 week' GROUP BY 1), sizes AS (SELECT cohort_week, COUNT(*) AS n FROM cohorts GROUP BY 1) -- grain: one row per cohort SELECT s.cohort_week::date AS cohort, s.n AS signups, CASE WHEN s.cohort_week + interval '1 week' < p.cur_week THEN ROUND(COALESCE(r.users, 0)::numeric / s.n, 3) END AS week1_retention FROM sizes s CROSS JOIN params p LEFT JOIN retained r ON r.cohort_week = s.cohort_week ORDER BY 1; Read it: NULL = that week is not finished yet; 0 = finished, nobody came back. Checks (not run, no connection): users.id unique; SUM(signups) = COUNT(*) of users in the window; rates <= 1; retained rows = distinct users (no fan-out). Confirm: any event counts as active; events with NULL user_id are excluded. Week 4 is the same query with interval '4 weeks'.

Both answers from the same model (Claude sonnet), 2026-10-16. The one without the skill was written in a separate session that never saw it. Shortened where marked with …

SKILL.md

First 40 lines

---
name: analytics-sql
description: "Use when the user says 'write a query for', 'weekly active users', 'retention by cohort', 'funnel conversion', 'why do these numbers not match', 'fix this slow report query', 'running total by month', or wants a readable analytics SQL query. Produces a CTE-per-step query with the grain stated per CTE, join fan-out checks, date spines, time zone and NULL handling, a note on how to read the result and the sanity checks that were run. Reads profile.md for database, ORM, data size and conventions; pattern library per dialect in patterns.md."
---

# Analytics SQL

Write analytics queries that a colleague can read top to bottom and that give the same number twice. Most wrong dashboards come from three causes: a join that multiplied rows, a date boundary that leaked a day, or a NULL that silently dropped a group. This skill writes the query so each of those is visible and checked.

## Before you start

Read `profile.md` in this folder:

| profile.md line | Use |
|---|---|
| **Database** | Picks the dialect for date functions, `QUALIFY`, `DATE_TRUNC`, percentile syntax. Section of the same name in `patterns.md`. |
| **ORM** | If an ORM is named, write the query as raw SQL for the reporting path and show where it plugs in (view, raw query, dbt model). "none, raw SQL" means deliver plain SQL. |
| **Data size** | Under 1M rows: clarity first. 1M-100M: filter early on the partition or date column, avoid `SELECT *`. 100M+: partition pruning, pre-aggregated daily tables, approximate distinct counts where the user accepts them. |
| **Conventions** | Naming (snake_case, plural tables, `_at` timestamps, `_id` keys) and style rules. Follow them over your defaults. |

If a line says "not set", ask once, write it into profile.md, continue. Open `patterns.md` and read the section for the database and the pattern you need.

## Workflow

1. **Restate the question as a metric.** Name the numerator, the denominator, the grain of the answer (per user per week?), the time window and the time zone. If two readings are plausible, state both and ask which one.
2. **Find the tables and keys.** Read the schema or ask for it. Write down each table's grain ("one row per order line") and its unique key. Never guess a key; run a uniqueness check (below).
3. **Build in CTEs, one step each.** Every CTE starts with a comment line `-- grain: one row per <thing>`. Order: filter source, derive, join, aggregate, final select. No subquery nested deeper than one level.
4. **Join with intent.** Name the key on both sides, state the expected cardinality (1:1, 1:many), and aggregate the "many" side to the grain of the "one" side before joining.
5. **Handle time deliberately.** Convert to the reporting time zone once, in the first CTE. Use half-open ranges `>= start AND < end`. Build a date spine when empty days must show as 0.
6. **Run the sanity checks** (below) if you have a database connection. If you cannot run anything, say so and list the checks for the user to run. Never claim a check passed that you did not run.
7. **Deliver** in the Output format.

## Decision table

| Situation | Do this | Not this |
|---|---|---|
| Count users after joining orders | `COUNT(DISTINCT user_id)`, or aggregate orders to one row per user first | `COUNT(*)` after a 1:many join |
| Date range for "March" | `>= '2025-03-01' AND < '2025-04-01'` | `BETWEEN ... AND '2025-03-31'` (drops 31 March after midnight) |
| Group with no activity should show 0 | Date spine `LEFT JOIN` facts, `COALESCE(n, 0)` | Inner join that drops the empty day |
| Average of a nullable column | State that `AVG` ignores NULL; use `COALESCE` only if NULL truly means 0 | Silent `AVG` over a mostly-NULL column |

Install

One command, in your project

It detects your agent; add --agent all for every agent.

npx proskills-cli add analytics-sql

Or copy the skill folder (the one holding SKILL.md) into the directory your agent reads, then restart it or start a new session.

AgentThis projectEvery project
Claude Code.claude/skills/~/.claude/skills/
Cursor.agents/skills/ (or .cursor/skills/)~/.cursor/skills/
Codex.agents/skills/~/.agents/skills/
Gemini CLI.agents/skills/ (or .gemini/skills/)~/.gemini/skills/

Changelog

What changed

  • v1.0.0 · 2026-10-16
    • First versioned release: Readable analytics queries you can trust.
    • Scanned 100/100 before release.

Keep it current with npx proskills-cli update; your profile.md is never touched.

Same collection

More from Data & SQL

The scan checks spec compliance, token cost, clarity and security patterns. A clean scan is not a guarantee: static scanners can be evaded. Scan any skill yourself.