Dataslope logoDataslope

Filtering Rows with WHERE

Keep only the SQLite rows you care about using WHERE, comparisons, AND, OR, NOT, BETWEEN, IN, and LIKE.

SELECT chooses columns. WHERE chooses rows.

That difference matters. A table may hold hundreds or millions of rows, but most questions only need some of them: books under a certain price, employees in one department, or customers in one city.

WHERE keeps matching rows

A WHERE clause is a test that SQLite applies to each row. If the test is true, the row stays in the result. If the test is false, the row is left out.

SQL
SQLite 3.53

The equals sign, =, means "is equal to." Text values go in single quotes.

Comparison operators

SQLite supports the comparisons beginners use constantly:

OperatorMeansExample
=equal togenre = 'Fiction'
<> or !=not equal togenre <> 'Travel'
< and >less than, greater thanprice < 20
<= and >=at most, at leastpages >= 300
BETWEEN a AND bfrom a through b, inclusiveprice BETWEEN 10 AND 20
IN (...)matches any value in a listgenre IN ('Fiction', 'Travel')
LIKEmatches a text patterntitle LIKE 'Data%'
SQL
SQLite 3.53
SQL
SQLite 3.53

BETWEEN includes both ends of the range. In the example, 200 and 320 would both count as matches.

QuestionSelect one

Which clause keeps only rows that pass a test?

SELECT

WHERE

ORDER BY

LIMIT

Combine conditions with AND, OR, and NOT

Real questions often have more than one part.

  • AND means both conditions must be true.
  • OR means at least one condition must be true.
  • NOT flips a condition.
SQL
SQLite 3.53
SQL
SQLite 3.53

When you mix AND and OR, use parentheses so your meaning is clear.

SQL
SQLite 3.53

IN is a shorter OR list

IN checks whether a value is one of several choices. It is often much cleaner than writing many OR conditions.

SQL
SQLite 3.53

LIKE finds text patterns

LIKE compares text to a pattern.

  • % means any number of characters, including none.
  • _ means exactly one character.

In SQLite, LIKE is case-insensitive for ordinary ASCII letters by default. That means 'sql%' can match SQL Street.

SQL
SQLite 3.53
SQL
SQLite 3.53

Check your understanding

QuestionSelect one

What does BETWEEN 10 AND 20 mean for a price?

More than 10 and less than 20, excluding both ends.

From 10 through 20, including 10 and 20.

Exactly 10 or exactly 20 only.

Any price except 10 through 20.

QuestionSelect one

Which condition keeps books that are either Fiction or Travel?

genre = 'Fiction' AND genre = 'Travel'

genre IN ('Fiction', 'Travel')

genre NOT IN ('Fiction', 'Travel')

genre LIKE 'Fiction Travel'

QuestionSelect one

In SQLite, what does the LIKE pattern 'Data%' match?

Only the exact text Data%.

Text that starts with Data, followed by any characters.

Text that ends with Data.

Only text with exactly one character after Data.

On this page