Balkrishna Attal

Home / Blog / Analytics

AnalyticsSeptember 28, 2026·8 min read

SQL for Product Managers: Six Ideas, Five Real Queries and the Traps to Avoid

Six ideas cover most PM questions. Five real queries with real outputs, the traps that quietly produce wrong numbers, and a two-week plan.

— views
SELECT channel, COUNT(*) FROM users;
Quick answer

A PM needs about six SQL ideas: SELECT/FROM, WHERE, GROUP BY, JOIN, CASE WHEN and date handling. Use LEFT JOIN so users without events stay in the denominator, put conditions in ON not WHERE, and count DISTINCT users rather than rows. Always sanity-check results, and write metric definitions down.

You do not need to become an analyst. You need to answer, on your own and in five minutes, questions like “what share of last month’s signups completed a first lesson?” Without SQL, that is a request to a colleague, a two-day wait, and often an answer that does not quite match the question. With about six concepts, you can do it yourself. This guide teaches exactly those, using a sample database and queries that I have actually run, so the outputs you see are real.

Why a PM should learn SQL

It also shows up in interviews. Some companies test SQL directly, and nearly all appreciate a PM who can reason about how data is structured. See the skills guide for where data fluency fits.

The sample data we will use

A small learning-app database with 600 users. Three tables:

TableColumnsOne row means
usersid, signup_date, channel, planOne person who signed up
eventsuser_id, event_name, event_dateOne action a user took (signup, first_lesson, lesson_completed, purchase, session)
ordersuser_id, amount, order_dateOne payment

Every query below was run against this data. The dates are in ISO format (YYYY-MM-DD). I used SQLite, so a few date functions differ from other databases; there is a translation table near the end.

The six ideas that cover most PM questions

SELECT / FROMChoose columns and a tableWHEREFilter rowsGROUP BYAggregate by categoryJOINCombine tablesCASE WHENIf-then inside a queryDatesCohorts and time windows
Master these and you can answer most product questions yourself.

Query 1: How many signups per month?

Start with the simplest useful shape: group rows and count them.

SELECT strftime('%Y-%m', signup_date) AS month,
       COUNT(*) AS signups
FROM users
GROUP BY month
ORDER BY month;
monthsignups
2026-01219
2026-02174
2026-03207

GROUP BY collapses many rows into one per group, and COUNT(*) counts the rows in each. This is the pattern behind almost every dashboard number.

Query 2: Activation rate by channel

Now a real product question: what share of users did a first lesson within 7 days of signing up, split by acquisition channel? This needs a JOIN between users and events.

SELECT u.channel,
       COUNT(DISTINCT u.id) AS signups,
       COUNT(DISTINCT e.user_id) AS activated,
       ROUND(100.0 * COUNT(DISTINCT e.user_id)
             / COUNT(DISTINCT u.id), 1) AS activation_pct
FROM users u
LEFT JOIN events e
       ON e.user_id = u.id
      AND e.event_name = 'first_lesson'
      AND julianday(e.event_date) - julianday(u.signup_date) <= 7
GROUP BY u.channel
ORDER BY activation_pct DESC;
channelsignupsactivatedactivation_pct
referral1208470.0
organic28815252.8
ads1927539.1

Read the result: referral users activate at 70.0%, while ads users activate at 39.1%. That gap is exactly the kind of insight an average would hide. Notice two details. I used LEFT JOIN so users with no matching event still count in the denominator; an ordinary JOIN would silently drop them and make every rate look 100%. And the conditions on event_name and the date window are in the ON clause, not in WHERE. Moving them to WHERE would also throw away the users who did not activate, which is a very common mistake.

Query 3: A funnel

SELECT event_name,
       COUNT(DISTINCT user_id) AS users
FROM events
WHERE event_name IN ('signup', 'first_lesson', 'lesson_completed', 'purchase')
GROUP BY event_name
ORDER BY users DESC;
event_nameusers
signup600
first_lesson384
lesson_completed243
purchase52

COUNT(DISTINCT user_id) counts each person once, however many times they did the action. That distinction matters enormously, as the next section shows. Reading down the list gives the shape of the funnel: many sign up, fewer start, fewer complete, fewer still pay.

Query 4: Retention by signup month

Here CASE WHEN flags whether a user had any activity at least 7 days after signing up, and we compute the share who did, by cohort.

SELECT strftime('%Y-%m', u.signup_date) AS cohort,
       COUNT(DISTINCT u.id) AS users,
       COUNT(DISTINCT CASE WHEN julianday(e.event_date) - julianday(u.signup_date) >= 7
                           THEN u.id END) AS retained,
       ROUND(100.0 * COUNT(DISTINCT CASE WHEN julianday(e.event_date) - julianday(u.signup_date) >= 7
                                        THEN u.id END)
             / COUNT(DISTINCT u.id), 1) AS retained_pct
FROM users u
LEFT JOIN events e ON e.user_id = u.id
GROUP BY cohort
ORDER BY cohort;
cohortusersretainedretained_pct
2026-0121911251.1
2026-021747543.1
2026-0320710249.3

This is a simple retention definition (“any activity 7 or more days later”). Many teams use stricter ones, such as “active exactly on day 7”. Whichever you use, write the definition down, because two people calculating “retention” differently will produce arguments instead of insight.

Query 5: Revenue per user by channel

SELECT u.channel,
       COUNT(DISTINCT u.id) AS users,
       COALESCE(SUM(o.amount), 0) AS revenue,
       ROUND(COALESCE(SUM(o.amount), 0) * 1.0 / COUNT(DISTINCT u.id), 1) AS revenue_per_user
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.channel
ORDER BY revenue_per_user DESC;
channelusersrevenuerevenue_per_user
referral1201148695.7
organic2881597455.5
ads192948849.4

COALESCE(SUM(...), 0) turns “no orders” (a missing value) into zero, so unpaid users do not break the maths. Revenue per user, split by channel, tells you which channels bring people who actually pay, which is usually a better guide to where to spend than signups alone.

The traps that produce wrong numbers

Trap 1: counting rows instead of people

Compare two versions of the same query. The first counts rows; the second counts distinct users.

SELECT u.channel, COUNT(*) AS activated
FROM users u
JOIN events e ON e.user_id = u.id
WHERE e.event_name IN ('first_lesson', 'lesson_completed')
GROUP BY u.channel;
channelactivated
ads159
organic311
referral157
SELECT u.channel, COUNT(DISTINCT u.id) AS activated
FROM users u
JOIN events e ON e.user_id = u.id
WHERE e.event_name IN ('first_lesson', 'lesson_completed')
GROUP BY u.channel;
channelactivated
ads95
organic189
referral100

The first version reports more “activated” users because someone who did both a first lesson and a completed lesson appears on two rows and is counted twice. The second is correct. Whenever you join to an events table, ask: could one person match several rows? If so, count distinct.

Trap 2: filtering in the wrong place on a LEFT JOIN

Put a condition on the right-hand table in WHERE and the LEFT JOIN quietly becomes an ordinary join, dropping the very rows you kept it for. Put the condition in ON instead, as in Query 2.

Trap 3: nulls and timezones

Comparisons with missing values return neither true nor false, so rows vanish from filters. Handle them explicitly with COALESCE or IS NULL. And dates recorded in different timezones can push events into the wrong day, which distorts daily and weekly metrics slightly. Agree one timezone for reporting.

If your database is not SQLite

The ideas are identical; only the date functions change. These are the translations for the two you will use most:

TaskSQLite (used here)PostgreSQLMySQL
Month of a datestrftime('%Y-%m', d)date_trunc('month', d)DATE_FORMAT(d, '%Y-%m')
Days between two datesjulianday(a) - julianday(b)a - b (for date types)DATEDIFF(a, b)

For BigQuery, use DATE_TRUNC and DATE_DIFF(a, b, DAY). When in doubt, search your database’s name with the task (“postgres days between dates”). Everyone does.

How to get good in two weeks

1Days 1-3ReadUnderstand the six ideas2Days 4-7Copy & runRun these queries3Days 8-11ChangeAlter them for new questions4Days 12-14Real dataAnswer a question you have
Typing queries beats reading about them.

Install a free database tool, load a small dataset (or recreate the sample tables above), and run each query in this article. Then modify them: activation within 3 days instead of 7, revenue by month, funnel by channel. Finally, ask one real question about your own product and answer it yourself. That last step is where it sticks.

Common mistakes

Trusting a query because it ran without errors, when it may still answer a different question. Counting rows when you mean people. Forgetting to exclude test users. Comparing numbers defined differently. And never sanity-checking: if a rate is above 100% or below 1%, the query is wrong until proven otherwise. Also worth saying: reading data does not replace judgment. A correct number can still mislead, which is the theme of the metric we almost shipped the wrong feature for.

Next: product analytics for PMs for the bigger picture, and the metrics reference for what to calculate.

Frequently asked questions

Do product managers need to know SQL?

It is not always required, but it is very useful. Basic SQL lets a PM answer their own data questions in minutes, define metrics precisely, and catch data errors, and some companies test it in interviews.

Which SQL concepts should a PM learn first?

SELECT and FROM, WHERE, GROUP BY with aggregate functions, JOIN, CASE WHEN, and date handling. Together these cover most product questions such as funnels, activation, retention and revenue by segment.

What is the difference between COUNT(*) and COUNT(DISTINCT user_id)?

COUNT(*) counts rows, so a user with several matching events is counted several times. COUNT(DISTINCT user_id) counts each user once, which is usually what product metrics need.

Why use a LEFT JOIN when calculating rates?

A LEFT JOIN keeps users who have no matching event, so they remain in the denominator. An ordinary JOIN drops them and inflates the rate.

Want this sharpened for your specific situation?

I take on a small number of career calls and resume reviews each month.

See how we could work together
BA

Balkrishna Attal

A product leader with a background in civil engineering. Writes about product management careers, interviews, and the frameworks that hold up in real work.