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:
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:
WHERE genre = 'fiction' AND (price < 10 OR year > 2021)| Write | Means |
|---|---|
year BETWEEN 2018 AND 2020 | year >= 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.
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'
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)
SELECT title, price
FROM books
WHEREShow 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)
SELECT title FROM books
WHEREShow the name of every customer who lives in Samarkand or Leeds. Use IN.
Tables you can query
- customers (id, name, city, joined, email)
SELECT name FROM customers
WHERE“`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.
WHEREkeeps only the rows whose condition is true.- Text is in single quotes;
=compares;<>means not equal. - Use brackets when you mix AND and OR.
BETWEENincludes both ends;INchecks a list;LIKEwith%matches patterns.
Filtering checks.
Which condition matches a title that starts with "The"?
- A`title = 'The'`
- B`title LIKE 'The%'`
- C`title LIKE '%The'`
- D`title IN 'The'`
Does year BETWEEN 2018 AND 2020 include 2020?
- AYes
- BNo
- COnly if you add `=`
- DOnly for text
How do you write "not equal" in SQL?
- A`!==`
- B`<>`
- C`=/=`
- D`NOT=`
How many rows of books have genre IN ('science', 'cooking')? (Count them from the table: 2 science and 2 cooking.)
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.