Choosing rows
After this: Keep the rows that pass a test with WHERE, join tests with AND, OR and NOT, and use IN, BETWEEN and LIKE.
On this page · 6 sections · 4 checks
In the last lesson you chose columns, and every student stayed in the result. Now the school is planning a trip in Leeds, and the office wants only the students who live there. That means choosing rows: keep some, leave out the rest.
A test for every row
You have already met the clause that does this, in the first tutorial: WHERE. After it comes a : a test that is true or false for each row. The database tries the test on every row and keeps the rows where it is true.
SELECT nameFROM studentsWHERE city = 'Leeds';| name |
|---|
| Ana |
| Chloe |
| Femi |
By hand, going through the six students:
- Ana lives in Leeds: true, keep.
- Ben lives in York: false, leave out.
- Chloe lives in Leeds: true, keep.
- Dev (Hull) and Eve (York): false, leave out.
- Femi lives in Leeds: true, keep.
Three rows pass. Text such as 'Leeds' goes in single quotes; numbers such as 2 do not.
The comparisons
| Write | Means |
|---|---|
= | equal to |
!= or <> | not equal to |
< and <= | less than, and less than or equal to |
> and >= | more than, and more than or equal to |
Two tests at once: AND, OR, NOT
AND joins two tests, and a row must pass both. Leeds students in year 2:
SELECT nameFROM studentsWHERE city = 'Leeds' AND year = 2;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Ana and Chloe pass both tests. Femi lives in Leeds but is in year 3, so the second test fails and his row is struck out.
OR keeps a row when at least one of the tests is true:
SELECT nameFROM studentsWHERE year = 1 OR city = 'Hull';| name |
|---|
| Ben |
| Dev |
| Eve |
Ben and Eve are in year 1, and Dev lives in Hull. NOT turns a test around, so true becomes false and false becomes true:
SELECT nameFROM studentsWHERE NOT city = 'Leeds';| name |
|---|
| Ben |
| Dev |
| Eve |
When one condition mixes AND and OR, put brackets around the part that belongs together, as in WHERE year = 1 AND (city = 'York' OR city = 'Hull'). Without brackets, AND is worked out before OR, which is not always what you mean.
Shorter tests: IN, BETWEEN, LIKE
IN checks a value against a list. The three students outside Leeds again, a third way:
SELECT nameFROM studentsWHERE city IN ('York', 'Hull');| name |
|---|
| Ben |
| Dev |
| Eve |
BETWEEN checks a range, and both ends count. year BETWEEN 2 AND 3 means the same as year >= 2 AND year <= 3:
SELECT nameFROM studentsWHERE year BETWEEN 2 AND 3;| name |
|---|
| Ana |
| Chloe |
| Dev |
| Femi |
LIKE checks text against a pattern. In the pattern, % stands for any run of characters, even none, and _ stands for exactly one character. 'e%' means "starts with e":
SELECT nameFROM studentsWHERE name LIKE 'e%';| name |
|---|
| Eve |
Eve starts with a capital E, and she still matches: in SQLite, LIKE ignores the difference between capital and small English letters. = does not. SQLite writes true as 1 and false as 0, and this comparison is false:
SELECT 'Leeds' = 'leeds';| 'Leeds' = 'leeds' |
|---|
| 0 |
So WHERE city = 'leeds', with a small l, gives back no rows at all.
Run it
Press Run. Then change the condition: find the year 2 students, the students whose name ends in e (LIKE '%e'), or everyone outside York. The rows come back in id order here; the next lesson explains why you should not count on that.
Check yourself
Which names does this query give back?
SELECT nameFROM studentsWHERE city = 'Leeds' AND year = 3;Two queries differ in one word. The first ends WHERE city = 'Leeds' AND year = 2; and the second ends WHERE city = 'Leeds' OR year = 2;. Which gives back more rows?
How many students does this query give back? Years: Ana 2, Ben 1, Chloe 2, Dev 3, Eve 1, Femi 3.
SELECT nameFROM studentsWHERE year BETWEEN 1 AND 2;Write the WHERE line so that the query gives back Ben, Dev and Eve: everyone who does not live in Leeds.
SELECT nameFROM students________________________ -- ← the missing line- Keep the rows that pass a test with WHERE: the test is tried on every row, and only rows where it is true stay.
- Write text in single quotes and numbers without them, and use =, != (or <>), <, <=, > and >=.
- Join tests with AND (both must be true) and OR (one is enough), turn one around with NOT, and use brackets when you mix AND and OR.
- Use IN for a list of values, BETWEEN for a range with both ends included, and LIKE with % and _ for a pattern.
- Say that in SQLite LIKE ignores capitals in English letters and = does not.