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.
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#
WHERE syntax#
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#
SELECT name, salary
FROM employees
WHERE salary > 60000;name | salary
------+-------
Ravi | 78000
Meera | 81000
Priya | 61000Filter by text#
Text values go in single quotes:
SELECT name, city
FROM employees
WHERE city = 'Mumbai';name | city
-----+-------
Asha | Mumbai
John | MumbaiComparison operators#
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.
SELECT name, department, salary
FROM employees
WHERE department = 'Sales' AND salary > 55000;name | department | salary
------+------------+-------
Priya | Sales | 61000SELECT name, city
FROM employees
WHERE city = 'Mumbai' OR city = 'Pune';name | city
------+-------
Asha | Mumbai
Meera | Pune
John | MumbaiShortcuts: IN, BETWEEN and LIKE#
IN: match any value in a list#
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#
SELECT name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 65000;name | salary
------+-------
Asha | 52000
Priya | 61000BETWEEN 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.
SELECT name
FROM employees
WHERE name LIKE 'M%';name
-----
MeeraFiltering 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 =:
-- 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
SELECT name, department, salary
FROM employees
WHERE department IN ('Engineering', 'Marketing')
AND salary < 80000;name | department | salary
-----+-------------+-------
Ravi | Engineering | 78000
John | Marketing | 46000Meera is in Engineering but earns 81,000, so she is filtered out.
Key takeaway#
Finished this lesson?