SQL SELECT with WHERE Clause — Filtering Rows
Master the SQL SELECT statement with WHERE clause for filtering rows. Covers comparison operators, LIKE, IN, BETWEEN, IS NULL, and boolean logic.
Detailed Explanation
Filtering Data with WHERE
The WHERE clause is how you filter rows returned by a SELECT statement. It supports comparison operators, pattern matching, set membership, and null checks.
Comparison Operators
SELECT * FROM employees
WHERE salary >= 50000
AND salary < 100000;
| Operator | Meaning |
|---|---|
= |
Equal to |
<> or != |
Not equal to |
<, > |
Less than, greater than |
<=, >= |
Less/greater than or equal |
Pattern Matching with LIKE
SELECT * FROM employees
WHERE name LIKE 'A%'; -- starts with A
WHERE email LIKE '%@gmail.com'; -- ends with @gmail.com
WHERE name LIKE '_a%'; -- second character is a
Set Membership with IN
SELECT * FROM employees
WHERE department_id IN (1, 3, 5);
Range with BETWEEN
SELECT * FROM orders
WHERE created_at BETWEEN '2025-01-01' AND '2025-12-31';
Null Checks
SELECT * FROM employees
WHERE manager_id IS NULL; -- no manager assigned
WHERE phone IS NOT NULL; -- has a phone number
Combining Conditions
Use AND, OR, and NOT to build complex filters. Parentheses control evaluation order:
SELECT * FROM employees
WHERE (department_id = 1 OR department_id = 3)
AND salary > 60000
AND name NOT LIKE 'Test%';
Use Case
You need to retrieve a filtered subset of records from a large table — for example, finding all active customers in a specific region whose accounts were created in the last 90 days.
Try It — SQL Cheat Sheet
Related Topics
SQL JOIN Types — INNER, LEFT, RIGHT, FULL, CROSS
Clauses & Filters
SQL GROUP BY and HAVING — Aggregating and Filtering Groups
Clauses & Filters
SQL ORDER BY and LIMIT — Sorting and Pagination
Clauses & Filters
SQL INSERT INTO VALUES — Adding Rows to a Table
DML Commands
SQL Aggregate Functions — COUNT, SUM, AVG, MIN, MAX
Functions