The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Common Operators
| Operator | Description |
|---|---|
= | Equal |
<> or != | Not equal |
> , < , >= , <= | Comparison |
BETWEEN | Between an inclusive range |
LIKE | Search for a pattern |
IN | Specify multiple possible values |
IS NULL / IS NOT NULL | NULL checks |
AND / OR | Combine multiple conditions |
The LIKE Operator
LIKE is used for pattern matching with two wildcards:
%— matches zero or more characters_— matches exactly one character
-- Names starting with 'A'
SELECT * FROM employees WHERE name LIKE 'A%';
-- Names with exactly 5 characters
SELECT * FROM employees WHERE name LIKE '_____';
NULL Handling
NULL represents an unknown or missing value. You cannot use = or != with NULL because any comparison with NULL evaluates to UNKNOWN (neither true nor false). Use IS NULL or IS NOT NULL instead.
SELECT * FROM employees WHERE email IS NULL;
Operator Precedence
When combining AND and OR, AND is evaluated first. Use parentheses to make your intent explicit:
-- Finds employees in dept 1 who earn > 50k OR any employee in dept 2
SELECT * FROM employees WHERE department_id = 1 AND salary > 50000 OR department_id = 2;
-- Same logic with parentheses for clarity
SELECT * FROM employees WHERE (department_id = 1 AND salary > 50000) OR department_id = 2;
WHERE with JOINs
The WHERE clause filters rows before grouping but after joins. If you filter on a column from the right table of a LEFT JOIN, you may accidentally turn it into an INNER JOIN:
-- This filters out users without orders (converts LEFT JOIN to INNER JOIN implicitly)
SELECT u.name, o.amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE o.amount > 100;
-- Correct: move the condition into the JOIN
SELECT u.name, o.amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id AND o.amount > 100;
Q: Difference between WHERE and HAVING?
A:
WHEREis used to filter individual rows before they are grouped.HAVINGis used to filter groups after theGROUP BYclause has been applied.
Q: How does SQL handle NULL in WHERE?
A: You cannot use = or != with NULL. You must use IS NULL or IS NOT NULL. This is because NULL represents an “unknown” value, and any comparison with it results in “unknown” (not true).
Q: What is the purpose of the LIKE operator?
A: The LIKE operator is used in a WHERE clause to search for a specified pattern in a column using wildcards like % (zero or more characters) and _ (exactly one character).
Q: Can a WHERE clause use a column alias from SELECT?
A: No. The WHERE clause executes before SELECT, so aliases defined in SELECT are not available. Use a subquery or CTE if you need to filter on a computed column.
1. Find employees with salary > 50,000.
SELECT * FROM employees
WHERE salary > 50000;2. Find employees with salary between 40k and 80k.
SELECT * FROM employees
WHERE salary BETWEEN 40000 AND 80000;3. Find names starting with ‘A’.
SELECT * FROM employees
WHERE name LIKE 'A%';4. Find records where email is NULL.
SELECT * FROM employees
WHERE email IS NULL;5. Select employees in departments 1, 2, and 3.
SELECT * FROM employees
WHERE department_id IN (1, 2, 3);6. Combine conditions with AND/OR.
SELECT * FROM employees
WHERE (department_id = 1 OR department_id = 2) AND salary > 50000;7. Find names with exactly 4 letters.
SELECT * FROM employees
WHERE name LIKE '____';Premium Content
Unlock WHERE Clause and all premium lessons with a subscription.
From ₹199.99/year — See plans