8.1 The WHERE Clause
Level: 8 | Version: v1.1 | Author: Meptrasoft
Overview
The WHERE clause is used to filter rows based on a condition.
For example:
SELECT *
FROM employees
WHERE dept_id = 10;
Only employees whose dept_id is 10 are returned.
Common filtering operators and clauses include:
WHERE
IN / NOT IN
BETWEEN
IS / IS NOT
LIKE / NOT LIKE
These will be covered one by one in this module.
WHERE → Keep only the rows that match a condition.

Practice Table
This topic uses the employees table:
| emp_id | emp_name | dept_id | salary | phone_no |
|---|---|---|---|---|
| 1 | John Smith | 10 | 45000 | 9876543210 |
| 2 | Emma Brown | 20 | 60000 | NULL |
| 3 | Samuel Reed | 10 | 32000 | 9123456780 |
| 4 | Anderson | 30 | 55000 | NULL |
| 5 | Sarah Wilson | 20 | 48000 | 9988776655 |
| 6 | Tom Watson | 10 | 51000 | 9090909090 |
Basic WHERE
Filter employees from department 10:
SELECT *
FROM employees
WHERE dept_id = 10;
Result:
| emp_id | emp_name | dept_id | salary |
|---|---|---|---|
| 1 | John Smith | 10 | 45000 |
| 3 | Samuel Reed | 10 | 32000 |
| 6 | Tom Watson | 10 | 51000 |
WHERE with a Numeric Condition
You can also filter numeric values.
SELECT *
FROM employees
WHERE salary > 50000;
Result:
| emp_id | emp_name | salary |
|---|---|---|
| 2 | Emma Brown | 60000 |
| 4 | Anderson | 55000 |
| 6 | Tom Watson | 51000 |
Common comparison operators include:
= → Equal
<> → Not equal
> → Greater than
< → Less than
>= → Greater than or equal
<= → Less than or equal
WHERE with Text
Text values are usually written inside single quotes:
SELECT *
FROM employees
WHERE emp_name = 'John Smith';
This returns only the row where the name matches exactly.
WHERE with Multiple Conditions
Conditions can be combined using AND and OR.
AND
Both conditions must be true.
SELECT *
FROM employees
WHERE dept_id = 10
AND salary > 40000;
OR
At least one condition must be true.
SELECT *
FROM employees
WHERE dept_id = 10
OR dept_id = 20;
AND → Both conditions
OR → At least one condition
Important: NULL Values
NULL means a value is unknown or missing.
Do not use:
WHERE phone_no = NULL
Instead, use:
SELECT *
FROM employees
WHERE phone_no IS NULL;
And for non-NULL values:
SELECT *
FROM employees
WHERE phone_no IS NOT NULL;
IS and IS NOT will be covered in detail later.
WHERE vs SELECT
A common beginner confusion:
SELECT → Which columns?
WHERE → Which rows?
For example:
SELECT emp_name, salary
FROM employees
WHERE dept_id = 10;
Here:
SELECTchoosesemp_nameandsalary.WHEREkeeps only employees from department10.
Common Beginner Mistakes
| ✗ Mistake | ✓ Correct Understanding |
|---|---|
WHERE chooses columns |
WHERE filters rows |
| Text values do not need quotes | String literals are normally written using single quotes |
= NULL checks for NULL |
Use IS NULL or IS NOT NULL |
AND means either condition can be true |
Both conditions must be true |
OR means both conditions must be true |
At least one condition must be true |
WHERE permanently removes rows |
It only filters the query result |
Placement Quick Points
WHERE
→ FILTER ROWS
=
→ EQUAL
>
→ GREATER THAN
AND
→ BOTH CONDITIONS
OR
→ AT LEAST ONE CONDITION
IS NULL
→ CHECK FOR NULL
WHEREfilters rows based on conditions.- Comparison operators can be used inside
WHERE. - Text values are normally enclosed in single quotes.
ANDrequires all combined conditions to be true.ORrequires at least one condition to be true.- Use
IS NULLandIS NOT NULLforNULLchecks. WHEREdoes not change the stored table.
Interview Questions
- What is the purpose of WHERE?
WHEREfilters rows based on a specified condition.- What is the difference between SELECT and WHERE?
SELECT → Chooses columns WHERE → Filters rows- How do you check for NULL?
WHERE phone_no IS NULL;- What is the difference between AND and OR?
ANDrequires all conditions to be true, whileORrequires at least one condition to be true.- Does WHERE modify the data?
No. It only filters the rows returned by the query.
- Can WHERE be used with text and numbers?
Yes.
WHERE department = 'IT'and:
WHERE salary > 50000
Practice & Hands-On Exercises
Using the employees table:
- Find employees from department
10:
SELECT * FROM employees WHERE dept_id = 10;
- Find employees with salary greater than
40000:
SELECT * FROM employees WHERE salary > 40000;
- Find the employee named
John Smith:
SELECT * FROM employees WHERE emp_name = 'John Smith';
- Find employees in department
10with salary greater than40000:
SELECT * FROM employees WHERE dept_id = 10 AND salary > 40000;
- Find employees in department
10or20:
SELECT * FROM employees WHERE dept_id = 10 OR dept_id = 20;
- Find employees whose
phone_noisNULL:
SELECT * FROM employees WHERE phone_no IS NULL;
- Explain the difference between
SELECTandWHERE. - Explain the difference between
ANDandOR.
💡 Tip: Test your queries using the in-browser interactive runner above.
Key Takeaway
SELECT → CHOOSE COLUMNS
WHERE → FILTER ROWS
The WHERE clause filters rows by applying conditions, allowing SQL to return only the data that matches the requirement.
End of The WHERE Clause