Checking your session…
Module 08: Data Filtering and Operators

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:

SQL
SELECT *
FROM employees
WHERE dept_id = 10;

Only employees whose dept_id is 10 are returned.

Common filtering operators and clauses include:

TEXT
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.

SQL WHERE Clause — Filtering Rows based on conditions
Figure 1: The WHERE clause filters table rows and returns only the records that satisfy 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:

SQL
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.

SQL
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:

TEXT
=    → 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:

SQL
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.

SQL
SELECT *
FROM employees
WHERE dept_id = 10
  AND salary > 40000;

OR

At least one condition must be true.

SQL
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:

SQL
WHERE phone_no = NULL

Instead, use:

SQL
SELECT *
FROM employees
WHERE phone_no IS NULL;

And for non-NULL values:

SQL
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:

TEXT
SELECT → Which columns?
WHERE  → Which rows?

For example:

SQL
SELECT emp_name, salary
FROM employees
WHERE dept_id = 10;

Here:

  • SELECT chooses emp_name and salary.
  • WHERE keeps only employees from department 10.

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

TEXT
WHERE
→ FILTER ROWS

=
→ EQUAL

>
→ GREATER THAN

AND
→ BOTH CONDITIONS

OR
→ AT LEAST ONE CONDITION

IS NULL
→ CHECK FOR NULL
  • WHERE filters rows based on conditions.
  • Comparison operators can be used inside WHERE.
  • Text values are normally enclosed in single quotes.
  • AND requires all combined conditions to be true.
  • OR requires at least one condition to be true.
  • Use IS NULL and IS NOT NULL for NULL checks.
  • WHERE does not change the stored table.

Interview Questions

What is the purpose of WHERE?

WHERE filters rows based on a specified condition.

What is the difference between SELECT and WHERE?
TEXT
SELECT → Chooses columns
WHERE  → Filters rows
How do you check for NULL?
SQL
WHERE phone_no IS NULL;
What is the difference between AND and OR?

AND requires all conditions to be true, while OR requires 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.

SQL
WHERE department = 'IT'

and:

SQL
WHERE salary > 50000

Practice & Hands-On Exercises

Using the employees table:

  1. Find employees from department 10:
SQL
SELECT * FROM employees WHERE dept_id = 10;
  1. Find employees with salary greater than 40000:
SQL
SELECT * FROM employees WHERE salary > 40000;
  1. Find the employee named John Smith:
SQL
SELECT * FROM employees WHERE emp_name = 'John Smith';
  1. Find employees in department 10 with salary greater than 40000:
SQL
SELECT * FROM employees WHERE dept_id = 10 AND salary > 40000;
  1. Find employees in department 10 or 20:
SQL
SELECT * FROM employees WHERE dept_id = 10 OR dept_id = 20;
  1. Find employees whose phone_no is NULL:
SQL
SELECT * FROM employees WHERE phone_no IS NULL;
  1. Explain the difference between SELECT and WHERE.
  2. Explain the difference between AND and OR.

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
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