Reading data
Filtering with WHERE
Goal: Filter rows with comparisons, AND/OR, IN, BETWEEN and LIKE · 9 min
WHERE keeps only the rows for which a condition is true:
- comparisons:
= <> < > <= >=(in SQL a single = compares) - combine:
AND,OR,NOT— use brackets when mixing AND and OR - lists:
city IN ('Berlin', 'Hamburg') - ranges:
price BETWEEN 50 AND 300(both ends included) - patterns:
name LIKE 'L%'(% = any characters, _ = exactly one) - missing values:
city IS NULL(never= NULL)
Text values go in single quotes.
Worked example: Affordable furniture
- Two conditions joined with AND.
- Desk (300) is excluded by price < 200.
- Result: Chair and Lamp.
SELECT name, price FROM products
WHERE category = 'furniture' AND price < 200
ORDER BY price;Output:
Lamp | 40.0
Chair | 150.0