Filtering with where
WHERE is the bouncer of SQL. It stands between your table and your results, only letting through rows that match your conditions. Without WHERE, you get everything. With it, you get exactly what you need.
How where filters rows
-- Customers from Sao Paulo
SELECT * FROM customers WHERE customer_state = 'SP' LIMIT 10;
-- Combine conditions with AND / OR
SELECT * FROM customers
WHERE customer_state = 'SP'
AND customer_city = 'sao paulo';
SELECT * FROM customers
WHERE customer_state = 'SP'
OR customer_state = 'RJ';Use = for exact matches. AND requires both conditions to be true; OR requires at least one.
Single quotes are for string values: WHERE state = 'SP'. Double quotes are for identifiers (table/column names) that have special characters or are case-sensitive: "Order Date". In practice, you will use single quotes 99% of the time. If your column name is simple lowercase, you never need double quotes.
-- IN: match any value in a list
SELECT * FROM customers
WHERE customer_state IN ('SP', 'RJ', 'MG')
LIMIT 10;
-- BETWEEN: inclusive range
SELECT * FROM order_items
WHERE price BETWEEN 50 AND 200
LIMIT 10;
-- LIKE: pattern matching (% = any characters)
SELECT * FROM customers
WHERE customer_city LIKE 'sao%'
LIMIT 10;
-- IS NULL: check for missing values
SELECT * FROM order_reviews
WHERE review_comment_message IS NULL
LIMIT 10;IN replaces multiple OR conditions. BETWEEN includes both endpoints. LIKE uses % as a wildcard. IS NULL finds missing values.
Matching exercise: Match the where operator
Loading practice…
Fill in the blanks: Complete the where clause
Loading practice…
Quiz: Quiz
Loading practice…
Code playground: Find specific customers
Loading practice…
Checkpoint: SQL basics check
Loading practice…