Skip to content
Laviyx Learn

Try

SQL course menu

SQL WHERE Clause: Filter Rows With Conditions

Learn how to filter rows in SQL with WHERE. Covers comparison operators, text and number filters, AND, OR, IN, BETWEEN, LIKE and NULL, with examples and exercises.

Beginner8 min readUpdated
On this page

A table can hold millions of rows, but you rarely need all of them. The WHERE clause lets you keep only the rows that match a condition, like "employees in Mumbai" or "orders over 500".

The example table#

idnamedepartmentsalarycity
1AshaSales52000Mumbai
2RaviEngineering78000Bengaluru
3MeeraEngineering81000Pune
4JohnMarketing46000Mumbai
5PriyaSales61000Delhi

WHERE syntax#

SQL
SELECT column1, column2
FROM table_name
WHERE condition;

The database checks the condition for every row. If it is true, the row is kept. If it is false (or unknown, more on that below), the row is left out.

Filter by a number#

SQLRun
SELECT name, salary
FROM employees
WHERE salary > 60000;
Output
name  | salary
------+-------
Ravi  | 78000
Meera | 81000
Priya | 61000

Filter by text#

Text values go in single quotes:

SQLRun
SELECT name, city
FROM employees
WHERE city = 'Mumbai';
Output
name | city
-----+-------
Asha | Mumbai
John | Mumbai

Comparison operators#

OperatorMeaningExample
=equal tocity = 'Pune'
<> or !=not equal todepartment <> 'Sales'
>greater thansalary > 50000
<less thansalary < 50000
>=greater than or equalsalary >= 61000
<=less than or equalsalary <= 61000

Note that SQL uses a single = for comparison, not == like many programming languages.

Combine conditions with AND and OR#

  • AND: both conditions must be true.
  • OR: at least one condition must be true.
SQLRun
SELECT name, department, salary
FROM employees
WHERE department = 'Sales' AND salary > 55000;
Output
name  | department | salary
------+------------+-------
Priya | Sales      | 61000
SQLRun
SELECT name, city
FROM employees
WHERE city = 'Mumbai' OR city = 'Pune';
Output
name  | city
------+-------
Asha  | Mumbai
Meera | Pune
John  | Mumbai

Shortcuts: IN, BETWEEN and LIKE#

IN: match any value in a list#

SQLRun
SELECT name, city
FROM employees
WHERE city IN ('Mumbai', 'Pune', 'Delhi');

This is a cleaner way to write several OR conditions on the same column. Use NOT IN to exclude a list.

BETWEEN: match a range#

SQLRun
SELECT name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 65000;
Output
name  | salary
------+-------
Asha  | 52000
Priya | 61000

BETWEEN includes both ends, so it is the same as salary >= 50000 AND salary <= 65000.

LIKE: match a text pattern#

LIKE uses two wildcard characters:

  • % matches any number of characters (including none).
  • _ matches exactly one character.
SQLRun
SELECT name
FROM employees
WHERE name LIKE 'M%';
Output
name
-----
Meera
PatternMatches
'M%'starts with M
'%a'ends with a
'%ee%'contains "ee" anywhere
'_o%'second letter is o

Filtering NULL values#

NULL means "no value" or "unknown". Imagine a new employee whose salary has not been entered yet. Because NULL is unknown, you cannot compare it with =:

SQLRun
-- This returns no rows, even if some salaries are NULL
SELECT name FROM employees WHERE salary = NULL;

-- This is the correct way
SELECT name FROM employees WHERE salary IS NULL;

Use IS NULL to find missing values and IS NOT NULL to exclude them.

Common mistakes#

Practice#

Practice exercise

Write a query that returns the name, department, and salary of employees who work in Engineering or Marketing and earn less than 80,000.

Show hint

Use IN for the two departments, then combine it with the salary condition using AND.

Show solution
SQLRun
SELECT name, department, salary
FROM employees
WHERE department IN ('Engineering', 'Marketing')
  AND salary < 80000;
Output
name | department  | salary
-----+-------------+-------
Ravi | Engineering | 78000
John | Marketing   | 46000

Meera is in Engineering but earns 81,000, so she is filtered out.

Key takeaway#

Finished this lesson?