Dataslope logoDataslope

Filtering Rows with WHERE

How to keep only the rows you care about using WHERE, comparison operators, and combinations with AND, OR, and NOT.

SELECT chooses columns. WHERE chooses rows. Together they let you cut a table down to exactly the slice you want — the most common thing you will ever do in SQL.

The idea: a test applied to every row

A WHERE clause is a condition — a true/false test. The database checks it against every row and keeps only the rows where the test comes out true.

A first filter

SQL
PostgreSQL 17

Only the two engineers survive the filter. Note = tests equality (SQL uses a single equals sign for "is equal to"), and the text goes in single quotes.

The comparison operators

You can compare with more than just equality:

OperatorMeansExample
=equal todept = 'Sales'
<> or !=not equal todept <> 'Sales'
< >less / greater thansalary > 90000
<= >=at most / at leastsalary >= 80000
BETWEEN a AND bwithin a range (inclusive)salary BETWEEN 70000 AND 90000
IN (...)matches any in a listdept IN ('Sales', 'Marketing')
LIKEtext pattern matchname LIKE 'A%'
SQL
PostgreSQL 17

Matching text patterns with LIKE

LIKE filters text by pattern. Two wildcards do the work:

  • % matches any number of characters (including none).
  • _ matches exactly one character.

So 'A%' means "starts with A," '%son' means "ends with son," and '%ar%' means "contains ar."

SQL
PostgreSQL 17

Combining conditions: AND, OR, NOT

Real questions often have several parts. Combine conditions with AND (both must be true), OR (at least one true), and NOT (flips a condition):

SQL
PostgreSQL 17

Mixing AND and OR? Use parentheses

When a condition mixes AND and OR, wrap the OR part in parentheses to make your intent unambiguous, e.g. WHERE dept = 'Sales' AND (salary > 75000 OR name LIKE 'C%'). AND binds more tightly than OR, and being explicit prevents surprising results.

How WHERE fits with SELECT

A useful way to picture a query: the database first figures out which rows pass WHERE, then picks the SELECT columns from those surviving rows.

We will refine this picture in the Composing Queries section, but "filter rows, then choose columns" is an excellent mental model to carry for now.

Check your understanding

QuestionSelect one

What is the role of a WHERE clause?

It chooses which columns appear in the result.

It keeps only the rows for which its condition is true.

It sorts the rows into order.

It permanently deletes rows that don't match.

QuestionSelect one

Which condition keeps employees whose salary is at least 80000?

WHERE salary > 80000

WHERE salary >= 80000

WHERE salary < 80000

WHERE salary = 80000

QuestionSelect one

In a LIKE pattern, what does the % wildcard match?

Exactly one character.

Any number of characters, including none.

Only digits.

The literal percent sign only.

QuestionSelect one

Which combination keeps rows where the department is Engineering and the salary is above 100000?

WHERE dept = 'Engineering' OR salary > 100000

WHERE dept = 'Engineering' AND salary > 100000

WHERE dept = 'Engineering' NOT salary > 100000

WHERE dept = 'Engineering', salary > 100000

Practice challenge

SQL Challenge
PostgreSQL 17
Books published before 2000

The books table has a year column. Return the title and year of every book published before the year 2000.

On this page