SQL for Product Managers
SELECT, FROM, WHERE: Asking a Precise Question
Everything in SQL starts with deciding what you're looking at, where it lives, and which rows actually qualify.
SELECT id, email, plan
FROM users
WHERE plan = 'pro';
A few habits that separate a query that works from a query that's actually good practice:
Never SELECT * in a query you intend to share or rely on. SELECT * grabs every column, including ones you don't need and ones you might misinterpret. Name the columns you actually want. It's more typing and considerably less risk of confusion.
Text values go in quotes, numbers don't. WHERE plan = 'pro' (quotes, because pro is text). WHERE amount > 100 (no quotes, because 100 is a number).
Comparisons you'll use constantly: = (equals), != or <> (not equals), > , < , >= , <= .
Operator | Meaning | Example |
|---|---|---|
= | Exactly equals | WHERE plan = 'pro' |
!= | Does not equal | WHERE plan != 'free' |
> / < | Greater than / less than | WHERE amount > 100 |
AND | Both conditions must be true | WHERE plan = 'pro' AND amount > 100 |
OR | Either condition can be true | WHERE plan = 'pro' OR plan = 'enterprise' |
IN | Matches any value in a list | WHERE plan IN ('pro', 'enterprise') |
LIKE | Text pattern matching | WHERE email LIKE '%@acme.com' |
IS NULL | The value is missing/empty | WHERE cancelled_at IS NULL |
That last one, IS NULL, trips up more people than any other line in this lesson. In SQL, a missing value is not the same as zero, an empty string, or false. It's its own special nothing, called NULL, and you cannot check for it with = NULL (that will silently fail to find anything). You have to write IS NULL or IS NOT NULL. This single fact causes more silently wrong queries than almost anything else in this course, so it's worth memorizing now.
YOUR MOVE THIS WEEK
Write one query, on paper if you don't have access to a real database yet, that answers a real question about your product using only SELECT, FROM, and WHERE. Something like "show me the email and signup date of every user on the enterprise plan."