Every query starts with SELECT. It's the verb that says 'give me back this data'. The simplest possible query:
SELECT * FROM customers;
Translation: give me every column (* means all columns) from the customers table. The output is the full table, every row.
Picking specific columns
SELECT first_name, last_name, country FROM customers;
Now we get only three columns. Always prefer naming columns over SELECT *. It's faster, more explicit, and survives schema changes.
WHERE: filtering rows
WHERE adds a condition. Only rows where the condition is true come back.
SELECT first_name, last_nameFROM customersWHERE country = 'Kenya';
Conditions can be combined with AND, OR, and NOT. Use parentheses when in doubt — operator precedence is real but not worth memorizing.
SELECT *FROM customersWHERE country = 'Kenya'AND signed_up_at > '2024-01-01'AND (plan = 'pro' OR plan = 'enterprise');
Common operators in WHERE
- = and != (or <>) for equality / inequality
- <, <=, >, >= for comparisons
- IN ('a', 'b', 'c') — membership in a list
- BETWEEN x AND y — inclusive range
- LIKE '%pattern%' — pattern matching with % (any chars) and _ (one char)
- IS NULL / IS NOT NULL — never use = NULL; NULL is the absence of a value, not a value itself
NULL is special
NULL = NULL is not true; it is unknown. Always use IS NULL / IS NOT NULL. This trips up beginners constantly. We'll cover NULL handling deeply in module 8.
Exercise
Write a query that returns customer email and country for customers in Kenya, Nigeria, or South Africa who signed up in 2024.