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.

OperatorMeaningExample
=Equal toprice = 100
>=Greater than or equal toprice >= 100
LIKEPattern matchname LIKE 'A%'
INMatches any value in a liststatus IN ('pending','shipped')

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');
Note: In UPDATE and DELETE statements, omitting WHERE affects every row in the table. Always double-check your condition before running an UPDATE or DELETE.
Note: Use LIKE with the % wildcard to match any sequence of characters and the _ wildcard to match exactly one character.

Exercise: MySQL Where

What is the main purpose of a WHERE clause?