Skip to content
Module

SQL for PMs: the queries that let you answer your own questions

Why PMs should read and write SQL, the core queries (funnel, cohort retention, top N), the classic traps to avoid and a free cheat sheet to keep.

Guide written by Alex Dionisio, Senior Product Manager. Updated November 13, 2026.

In short

A Product Manager does not need to become an analyst to get value from SQL. Reading a data model and writing a handful of queries (counting, filtering a date range, tracking a funnel, measuring retention, ranking a top N) is enough to answer most everyday questions alone, as long as you know the classic traps, work with read-only access and get anything behind a major decision reviewed.

Why should a Product Manager learn SQL?

Learning SQL lets a Product Manager answer simple questions about product usage without waiting for an analyst to free up, and understand the numbers behind dashboards well enough to know when to be skeptical of them.

SQL is the language used to query relational databases. In most digital products, sign-ups, subscriptions and user actions end up in tables you can query with a few keywords: SELECT, FROM, WHERE, GROUP BY, JOIN.

For a PM, the point is not to replace the data team. The value lies elsewhere:

  • Save time on small questions: how many accounts used the new option last week? That fits in three lines and does not deserve a ticket.
  • Understand where numbers come from: every dashboard chart is a hidden query. Being able to read it helps you spot a missing filter or a questionable definition.
  • Write better requests: when you do need an analyst, you already know which tables exist and which precise question to ask.
  • Check a hunch before raising it: confirm a problem is real before putting it on a steering committee agenda.

SQL complements product analytics: analytics tools give you ready-made views, SQL answers the questions no view was built for.

How do you understand the data model before writing a query?

Before writing a query, a PM needs to know which tables exist, what a single row represents in each one, which column uniquely identifies a row (the primary key) and which columns link tables together (foreign keys). Without that map, results can be wrong with no warning at all.

The data model describes how information is stored. Throughout this guide we use a deliberately simple example, an online project management tool:

  • users: one row per account, with id, signup_at, country and plan.
  • events: one row per tracked action, with user_id, name (for example project_created) and occurred_at.
  • subscriptions: one row per paid subscription, with user_id, started_at and ended_at.

The most useful question to ask about any table is: what exactly does one row represent? An account, an action, a subscription, a day? Everything else follows. If an events table holds one row per click, counting its rows does not give you a number of users.

Keys create the links. users.id uniquely identifies an account; events.user_id points back to it. That match is what lets a join connect an action to the person who performed it. Ask the engineering team for the schema or table documentation before you start: it is often the most valuable document you will read that week. The lesson Reading a data model walks through this step by step.

Which basic queries do PMs use most often?

The three basic queries a PM relies on are counting distinct users, filtering on a date range and ranking the most frequent items (top N). Together they cover a large share of everyday questions about how a product is used.

Count users, not rows. To know how many accounts created a project, count distinct IDs; otherwise one heavy user weighs as much as ten occasional ones:

SELECT COUNT(DISTINCT user_id) AS creators
FROM events
WHERE name = 'project_created';

Filter a date range. Use an inclusive start date and an exclusive end date. This pattern avoids silently dropping the last day when the column also stores a time:

SELECT COUNT(*) AS signups
FROM users
WHERE signup_at >= '2026-10-01'
  AND signup_at <  '2026-11-01';

Rank a top N. To see which actions reached the most users over the last seven days (today excluded, since the day is incomplete), group, count, sort and limit:

SELECT name, COUNT(DISTINCT user_id) AS users
FROM events
WHERE occurred_at >= CURRENT_DATE - 7
  AND occurred_at <  CURRENT_DATE
GROUP BY name
ORDER BY users DESC
LIMIT 10;

Date syntax differs between databases (PostgreSQL, BigQuery, Snowflake, MySQL). The examples above use PostgreSQL; adjust date functions to your own tool.

How do you join users and events and track a simple funnel?

To combine users and events, you write a join on the shared key (the account ID). For a simple funnel, you start from the accounts that signed up in a period and count how many of them completed each step, then compare the steps with one another.

A join lets you, for instance, break down project creators by plan, a piece of information stored in users while the action lives in events:

SELECT u.plan, COUNT(DISTINCT e.user_id) AS creators
FROM events e
JOIN users u ON u.id = e.user_id
WHERE e.name = 'project_created'
GROUP BY u.plan;

A simple conversion funnel needs one count per step:

SELECT
  COUNT(DISTINCT u.id) AS signed_up,
  COUNT(DISTINCT CASE WHEN e.name = 'project_created' THEN e.user_id END) AS project,
  COUNT(DISTINCT CASE WHEN e.name = 'invite_sent' THEN e.user_id END) AS invited
FROM users u
LEFT JOIN events e
  ON e.user_id = u.id
 AND e.occurred_at >= u.signup_at
 AND e.occurred_at <  u.signup_at + INTERVAL '30 days'
WHERE u.signup_at >= '2026-10-01'
  AND u.signup_at <  '2026-11-01';

Start from accounts that signed up in the period and look at what they do in their first thirty days, so every step covers the same people. This version does not enforce step order: someone who sends an invite before creating a project is counted in both columns. That is often fine for a first read; for a high-stakes decision you need an ordered funnel, which your data team or the course can help you build. Join mistakes are the number one source of wrong numbers, and the lesson Join pitfalls covers them.

How do you measure cohort retention in SQL, the simple way?

A simplified cohort retention query groups users by sign-up month, then counts, within each group, those who are still active in a fixed window after signing up, for example between day 30 and day 60.

Cohort analysis avoids a common bias: overall retention mixes users who joined two years ago with users who joined yesterday. Grouping by arrival date shows whether newer cohorts stick around better or worse than older ones.

SELECT
  DATE_TRUNC('month', u.signup_at) AS cohort,
  COUNT(DISTINCT u.id) AS signed_up,
  COUNT(DISTINCT e.user_id) AS active_d30_d60
FROM users u
LEFT JOIN events e
  ON e.user_id = u.id
 AND e.occurred_at >= u.signup_at + INTERVAL '30 days'
 AND e.occurred_at <  u.signup_at + INTERVAL '60 days'
WHERE u.signup_at < CURRENT_DATE - 60
GROUP BY 1
ORDER BY 1;

Three things to keep in mind before reading the output:

  • The LEFT JOIN keeps every user who signed up, including inactive ones; a plain join would drop them and inflate retention.
  • Recent cohorts have not reached day 60 yet: their number would be incomplete, not bad, hence the filter that keeps only users who signed up more than sixty days ago.
  • How you define "active" (any event or one specific action) changes everything. Write the definition right next to the number.

What are the classic SQL traps for PMs?

The classic SQL traps for PMs are duplicates created by a join, mishandled time zones, null values that quietly vanish from calculations and conclusions drawn from a sample that is too small or unrepresentative. Each one produces a number that looks plausible and is wrong.

  • Duplicates: joining a users table to a table where each user has several rows multiplies the rows. A SUM over the result then counts the same amount several times. Habit: compare row counts before and after every join.
  • Time zones: timestamps are often stored in UTC. A "day" computed in UTC is not your users' day in Paris or New York, which shifts peaks and day-over-day comparisons. In PostgreSQL, a date written without a time zone and DATE_TRUNC both use the session time zone: set it explicitly, for example SET TIME ZONE 'America/New_York';, or write '2026-10-01 00:00:00+00'.
  • Nulls: NULL means "unknown", not zero. A condition like WHERE country <> 'US' also excludes rows where the country is missing, and AVG skips nulls without telling you.
  • Sample size: a gap observed on a few dozen accounts may be pure chance. Check the counts before concluding and, to compare two variants, use a proper A/B test approach.
  • Test and internal accounts: your own team's accounts skew small measurements. Ask how to exclude them.

A good habit is to check every result through a second route: a dashboard figure, a plausible estimate, or the same question asked differently. If the two disagree, suspect the query first.

How can you use AI to write SQL without getting it wrong?

AI writes SQL queries that look right very quickly, but it does not know your data model or your business definitions. Give it the schema and metric definitions, ask it to explain the query, then check the output against a case you already know.

An AI assistant is a great partner for turning a plain-language question into a query, or for decoding an existing one. Its typical mistakes are predictable: it invents a column that does not exist, picks a join that duplicates rows or interprets "active" its own way. A simple method:

  1. Provide context: the relevant tables, their columns, what one row represents, your database's SQL dialect.
  2. Ask a precise question: period, population, definition of each metric, what to exclude.
  3. Ask it to walk you through each line and list its assumptions, not just hand over code.
  4. Test on a case you know: a specific user, a short period, a number already validated elsewhere.
  5. Get a review for any query feeding a significant decision or a number shown to leadership.

Never paste real personal data into an AI tool your company has not approved: the schema is enough to write the query. The lesson Writing SQL with AI and the course AI for working with data go deeper. The same verification mindset applies when you assess an AI feature, as the guide on AI evals for Product Managers explains.

What data access should a PM ask for?

A PM should ask for read-only access, ideally to a copy of production data (a warehouse or a replica) rather than the database serving the product, with personal data masked or limited to what is strictly needed.

Data access is a security and compliance question before it is a technical one. A few principles worth defending with your team:

  • Read-only: an account that cannot modify or delete anything. A badly written UPDATE or DELETE should simply be impossible to run.
  • Away from the live product database: an expensive query there can slow the app down for everyone. A data warehouse or replica removes that risk.
  • Minimal data: no emails, names or contact details unless the question requires them. GDPR sets a data minimisation principle: process only the data your stated purposes actually need.
  • Traceability: a personal login, not a shared one, so it is clear who looked at what.

The lesson Access and confidentiality helps you prepare that request. To go from data model to product queries with guided exercises, the course Data and SQL for PMs covers the full approach, and the course Product analytics helps you choose the right metrics before you compute them.

Free template: REQUETES-SQL-PM.md

A cheat sheet to keep open next to your query tool. Swap table and column names for those in your own data model, then fill in the "Definitions" section with your team.

Download REQUETES-SQL-PM.md
# SQL cheat sheet for PMs

## My data model
- Accounts table: [name] (one row = [what])
- Events table: [name] (one row = [what])
- Join key: [events_table.column] -> [accounts_table.column]
- SQL dialect: [PostgreSQL / BigQuery / Snowflake / other]
- Timestamp time zone: [UTC or other]

## Definitions (agree with the team)
- Active user: [event(s) that count]
- Excluded accounts: [internal, test]

## Core queries
```sql
-- Count distinct users
SELECT COUNT(DISTINCT user_id) FROM events WHERE name = '[action]';

-- Date range (start inclusive, end exclusive)
WHERE occurred_at >= '[YYYY-MM-DD]' AND occurred_at < '[YYYY-MM-DD]'

-- Top N
SELECT name, COUNT(DISTINCT user_id) AS n
FROM events GROUP BY name ORDER BY n DESC LIMIT 10;

-- Join accounts / events
SELECT u.[column], COUNT(DISTINCT e.user_id)
FROM events e JOIN users u ON u.id = e.user_id
GROUP BY u.[column];
```

## Before sharing a number
- [ ] I know what one row of each table represents
- [ ] I counted distinct IDs, not rows
- [ ] Row count checked before and after each join
- [ ] Date bounds and time zone checked
- [ ] Null values accounted for
- [ ] Internal and test accounts excluded
- [ ] Sample large enough to conclude
- [ ] Result cross-checked with another source
- [ ] Query reviewed if the number drives a major decision

## Working with AI
- Provide: schema, dialect, definitions, period, exclusions
- Ask for: the query, what each line does, the assumptions made
- Never paste real personal data

Put it into practice

The “Model your product’s data and query it with SQL” course walks you through it step by step, on your own product, all the way to the deliverable.

See the course

Frequently asked questions

How long does it take a PM to learn SQL?

The useful basics (selecting, filtering, counting, grouping, joining two tables) come fairly quickly if you practice on your own data. The slow part is not the syntax but learning your product's data model and its traps.

Should a PM write SQL or just be able to read it?

Reading it is the minimum: it lets you understand a dashboard and talk with the data team. Writing it, even with AI help, makes you self-sufficient on simple questions, as long as you follow the verification rules.

Which SQL should I learn: PostgreSQL, MySQL or BigQuery?

The core language is shared by every database. What changes between databases is mainly how they handle dates and strings. Learn the one your company uses and keep its documentation close at hand.

Can I let AI write all my SQL queries?

AI can write most queries, but you remain accountable for the number. Give it the schema and definitions, ask it to explain each line and test the output on a known case before sharing it.

Does SQL replace a product analytics tool?

No, they complement each other. Product analytics tools offer ready-made, easy-to-share views; SQL answers the questions those views did not anticipate and lets you check how they are calculated.