WHERE Clause
In this page:
Introduction to Filtering
The WHERE clause filters a result set down to only the rows matching a specified condition, turning a query that would return an entire table into one that returns exactly the records you actually need.
Example: Introduction to Filtering
SELECT * FROM users WHERE age > 18;
Filtering with Numbers
Numeric filtering uses standard comparison operators like >, <, and >= directly against a column, which is the typical way to query things like 'all products under $50' or 'users older than 18'.
Example: Filtering with Numbers
SELECT * FROM products WHERE price < 20;
Filtering with Text
Text values in a WHERE condition must be wrapped in single quotes, or MySQL will misinterpret the bare word as a column name instead of a literal string and raise an error.
Example: Filtering with Text
SELECT * FROM users WHERE name = 'Amit';
Filtering with Dates
Date literals also need to be quoted like text, written in the YYYY-MM-DD format MySQL expects, since an unquoted date would similarly be parsed as an invalid column reference.
Example: Filtering with Dates
SELECT * FROM orders WHERE order_date = '2024-03-05';
Using Inequality Operators
The != or <> operator excludes rows matching a specific value rather than selecting for it, which is the standard way to express 'everything except this one case' in a filter.
Example: Using Inequality Operators
SELECT * FROM users WHERE status != 'inactive';
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: