MySQL Where
The WHERE clause filters rows in a SELECT, UPDATE, or DELETE statement so that only records matching a condition are affected.
Filtering Rows with WHERE
The WHERE clause is placed after FROM and before ORDER BY, and it restricts which rows a statement processes. It works with SELECT to filter what is returned, and with UPDATE and DELETE to control exactly which rows are changed or removed.
Comparison Operators
- = and <> (or !=) for equality and inequality
- > , < , >= , <= for numeric and date comparisons
- BETWEEN ... AND ... for an inclusive range
- LIKE for pattern matching with wildcards
- IN (...) to match any value in a list
- IS NULL / IS NOT NULL to test for missing values
String and date values must be wrapped in quotes, for example WHERE country = 'Germany', while numeric values are written without quotes. Multiple conditions can be combined with AND, OR, and NOT, and parentheses can be used to control the order in which they are evaluated.
Query Examples
Filter by an exact match
SELECT * FROM customers WHERE country = 'Germany';Combine multiple conditions with AND
SELECT * FROM products WHERE price >= 100 AND category = 'Electronics';Match against a list of values with IN
SELECT * FROM orders WHERE status IN ('pending', 'shipped');Exercise: MySQL Where
What is the main purpose of a WHERE clause?