The WHERE clause filters rows so that only those matching a condition appear in your results. Without WHERE, a SELECT returns every row in the table. Learning to write precise filters is essential for working with real-world datasets that may contain millions of records.
Comparison Operators
MySQL supports the standard comparison operators: =, != (or <>),<, >, <=, and >=. Use them to compare column values against literals or other columns.
-- Employees earning more than 60000
SELECT first_name, salary
FROM employees
WHERE salary > 60000;
-- Orders placed on a specific date
SELECT order_id, total
FROM orders
WHERE order_date = '2025-01-15';Combining Conditions with AND / OR
Chain multiple conditions with AND (all must be true) or OR (at least one must be true). Use parentheses to control evaluation order and avoid ambiguity.
SELECT *
FROM employees
WHERE department = 'Engineering'
AND salary >= 70000;
SELECT *
FROM products
WHERE (category = 'Electronics' OR category = 'Books')
AND price < 50;Pattern Matching with LIKE
The LIKE operator matches string patterns. Use % to represent any sequence of characters and _ to represent exactly one character.
-- Names starting with 'J'
SELECT first_name
FROM employees
WHERE first_name LIKE 'J%';
-- Exactly 5-character codes
SELECT code
FROM products
WHERE code LIKE '_____';The IN Operator
IN checks whether a value matches any item in a list. It is cleaner than writing multiple OR conditions and works with subqueries too.
SELECT *
FROM orders
WHERE status IN ('pending', 'processing', 'shipped');BETWEEN for Ranges
BETWEEN is inclusive on both ends and works with numbers, dates, and strings. It is equivalent to using >= and <= together but reads more naturally.
SELECT *
FROM employees
WHERE hire_date BETWEEN '2024-01-01' AND '2024-12-31';Handling NULL Values
NULL represents missing or unknown data. You cannot compare NULL with =; instead useIS NULL or IS NOT NULL. Forgetting this is one of the most common beginner mistakes.
SELECT *
FROM employees
WHERE manager_id IS NULL;Try this query in UnifySQL
Write, optimize, and collaborate on MySQL queries with AI assistance.
Start Free