SELECT, WHERE and CASE

SELECT, WHERE, and CASE are the basic tools for asking a database a question and shaping the answer.

SELECT chooses which columns you get back. Write SELECT * and you get every column; name columns, like SELECT country, plan, and you get only those. SELECT controls the shape of the result, not which rows are included.

WHERE filters which rows you get back. It keeps only rows that meet a condition and drops the rest. For example, WHERE plan = 'pro' keeps only paid users, and WHERE country = 'US' keeps only users in the US. This is the key split: SELECT picks columns, WHERE picks rows.

CASE creates a new derived column by labeling or bucketing rows with conditions. The pattern is CASE WHEN ... THEN ... ELSE ... END. For example, you can compute a label called tier: return 'paid' when plan = 'pro', otherwise 'free'. Or you can bucket a number into 'low', 'medium', or 'high'. CASE does not change the stored data; it computes a value on the fly for the result.

One habit to avoid: the ELSE here labels anything that is not 'pro' as 'free', including a NULL plan or a typo like 'Pro'. A safer pattern ends with ELSE 'unknown', so bad data shows up as unknown instead of silently joining a real bucket.

These three show up in everyday SQL. Most queries start by choosing columns with SELECT, filtering rows with WHERE, and sometimes adding a CASE label, before any joining or grouping happens.

Filtering rows live

Filtering

Selecting columns, filtering rows, bucketing with CASE

user_idcountryplansignup_date1GBpro2025-02-152FRpro2025-01-233GBfree2025-02-224GBfree2025-02-095USpro2025-01-086ESfree2025-01-207FRfree2025-02-108ESfree2025-02-19
select
where
caseCASE WHEN plan = 'pro' THEN 'paid' ELSE 'free' END AS tier
SELECT user_id, country, plan, signup_date
FROM users;

SELECT picks columns, WHERE picks rows, and CASE derives a new column per row. None of them change the stored table; they shape what the query returns.

all 8 rows shown
columns
4
rows kept
8 of 8
case
off