Skip to content
Lesson 2 of 10 · SQL for Beginners

Filtering rows with WHERE

Keep only the rows you want with comparisons, AND, OR, IN, BETWEEN and LIKE.

17 min

WHERE goes after FROM and keeps only the rows for which its condition is true:

sql1 line
SELECT title, price FROM books WHERE genre = 'fiction';

Text values go in single quotes. Numbers do not. (Double quotes are for column names, not text.)

=, <> (or !=), <, <=, >, >=. Notice SQL uses a single = to compare.

AND needs both, OR needs either, NOT flips one. When you mix AND and OR, add brackets so you and the database agree:

sql1 line
WHERE genre = 'fiction' AND (price < 10 OR year > 2021)

WriteMeans
year BETWEEN 2018 AND 2020year >= 2018 AND year <= 2020: both ends included
genre IN ('science', 'cooking')genre = 'science' OR genre = 'cooking'
title LIKE 'The%'starts with The
title LIKE '%Rain%'contains Rain
author LIKE 'M._Hale'_ matches exactly one character

In LIKE, % matches any run of characters (including none). In SQLite, LIKE ignores capitals for plain English letters.

Reference

Which operator for which question

Books priced under $10?

Condition
price < 10

Books by M. Hale or R. Okafor?

Condition
author IN ('M. Hale', 'R. Okafor')

Published 2017 to 2019?

Condition
year BETWEEN 2017 AND 2019

Title begins with S?

Condition
title LIKE 'S%'

Not fiction?

Condition
genre <> 'fiction'
Exercise1 check
sql

Show the title and price of every fiction book that costs less than 13.50.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code3 lines
SELECT title, price
FROM books
WHERE
Exercise1 check
sql

Show the title of every book published from 2018 to 2020 (both years included), or whose title contains the word Salt.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code2 lines
SELECT title FROM books
WHERE
Exercise1 check
sql

Show the name of every customer who lives in Samarkand or Leeds. Use IN.

Tables you can query

  • customers (id, name, city, joined, email)
starter code2 lines
SELECT name FROM customers
WHERE
Common misconception

“`WHERE genre = "fiction"` with double quotes is fine.”

Many programming languages use double quotes for text, and some databases accept it.

What is actually true

In standard SQL, single quotes mean text and double quotes mean a name. SQLite tolerates the mistake when no column has that name, which makes it a dangerous habit: the query may silently compare against the wrong thing elsewhere.

How to catch yourself

Always write text with single quotes.

  • WHERE keeps only the rows whose condition is true.
  • Text is in single quotes; = compares; <> means not equal.
  • Use brackets when you mix AND and OR.
  • BETWEEN includes both ends; IN checks a list; LIKE with % matches patterns.
Check yourself

Filtering checks.

1Multiple choice

Which condition matches a title that starts with "The"?

  • A`title = 'The'`
  • B`title LIKE 'The%'`
  • C`title LIKE '%The'`
  • D`title IN 'The'`
2Multiple choice

Does year BETWEEN 2018 AND 2020 include 2020?

  • AYes
  • BNo
  • COnly if you add `=`
  • DOnly for text
3Multiple choice

How do you write "not equal" in SQL?

  • A`!==`
  • B`<>`
  • C`=/=`
  • D`NOT=`
4Numeric answer

How many rows of books have genre IN ('science', 'cooking')? (Count them from the table: 2 science and 2 cooking.)

5Multiple choice

In WHERE a = 1 OR b = 2 AND c = 3, which runs first?

  • A`a = 1 OR b = 2`
  • B`b = 2 AND c = 3`
  • CLeft to right, whatever
  • DIt is an error

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.