SELECT and WHERE: building a precise first question
Half of SQL is what to see (SELECT) and which rows to see (WHERE). Getting those two right answers most everyday questions.
SELECT picks columns, WHERE picks rows
After SELECT, list the columns you want. The star means all of them — useful for a quick look, less so when you share a result. AS gives a column a readable alias in the output.
WHERE tests each row and keeps the true ones. Quote strings with single quotes and leave numbers bare. If dates are stored as TEXT, compare them as text: '2026-09-01'.
SELECT order_id, order_date, revenue
FROM orders
WHERE status = 'completed' AND revenue >= 1000000;
How AND, OR and NOT bind
AND binds tighter than OR, so A OR B AND C means A OR (B AND C). Whenever your intent differs — or a reader might wonder — add parentheses. They document the intent as much as they change the result.
NOT negates a whole condition. Note that NOT status = 'refunded' still excludes rows where status is NULL, because a comparison with NULL is never true.
| Condition | Reads as | Recommendation |
|---|---|---|
| a OR b AND c | a OR (b AND c) | add parentheses even when equal |
| NOT a = 1 | (NOT a = 1) | a <> 1 is easier to read |
| status <> 'refunded' | NULL rows excluded | add OR status IS NULL if wanted |
| revenue > 0 | NULL rows excluded | state IS NOT NULL explicitly |
LIKE, IN and BETWEEN
LIKE matches a pattern: % is any run of characters, _ is exactly one. 'C-%' finds every value that starts with C-. In SQLite, LIKE is case-insensitive for ASCII by default, unlike the rest of the comparison operators.
IN tests membership in a list, BETWEEN tests an inclusive range, so BETWEEN 100 AND 200 includes both ends. Both turn long OR chains into something a reader can scan.
SELECT * FROM orders
WHERE customer_id LIKE 'C-20%'
AND channel IN ('online', 'store')
AND quantity BETWEEN 2 AND 5;
Check the filter, not just the result
After writing a WHERE clause, test each condition on its own: total rows, rows per condition, rows for the combination. The step that filters too much is immediately visible. When a result is empty, the fastest debugging move is to keep one condition and run again.
Questions
How do I compare case-sensitively?
SQLite compares text byte by byte, so 'Seoul' and 'seoul' differ. Only LIKE ignores case, and only for ASCII.
Can WHERE use a SELECT alias?
No. An alias belongs to the same SELECT level. Wrap the query in a subquery or a WITH clause to filter on it.
Sources
Every query in this guide runs against the built-in sample data.
Open the lab