Use the WHERE clause with comparison, logical, or pattern operators to return only rows matching specified conditions.
Filtering means using the WHERE clause in SELECT, UPDATE, or DELETE statements to restrict rows returned or affected based on column values.
The WHERE clause evaluates each row and returns it only if the condition resolves to TRUE.
Use numeric or string comparisons to match exact or range values, for example price > 50.
Combine multiple conditions to build complex filters.
Match substrings or regular expressions for flexible searches.
Test for membership, ranges, or NULL values concisely.
SELECT id, customer_id, total_amount FROM Orders WHERE total_amount > 100;
SELECT * FROM Products WHERE stock < 10 AND price <= 20;
Yes.Place the filter after the JOIN or in an ON clause to define row matching.
SELECT o.id, c.name, o.total_amount
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
WHERE o.order_date >= '2024-01-01';
Index frequently filtered columns, avoid functions on indexed columns, use prepared statements to prevent SQL injection, and prefer = or IN over LIKE when possible.
.
Filtering can be fast when the filtered columns are indexed. Without indexes, MySQL must scan the entire table, increasing response time.
MySQL string comparisons are case-insensitive when the column collation ends with _ci
. For explicit control, use LOWER()
and compare against lowercase literals.