Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

AC 2.1: Filtering with BETWEEN, AND, OR and NOT

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

55 minutes

One test is rarely enough

A pupil wants a cold drink that costs between 100 and 150 pence. Another wants a snack that is not crisps.

Each wish is two or three tests joined together. Small code words can join tests.

Key words

WordWhat it means
Databasean organised store of tables that you can ask questions
SQLthe language used to ask a database for data
Queryone question written in SQL
Conditiona test that is true or false for each row
BETWEENtests that a value is inside a range, edges included
ANDtrue only when both tests are true
ORtrue when at least one test is true
NOTturns true into false and false into true
Bracketsround ( ) marks that make SQL work out one part first

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can combine tests with BETWEEN, AND, OR, NOT and brackets to pick the right rows.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

BETWEEN picks a range

BETWEEN 100 AND 150 keeps every value from 100 up to 150. Both edges are included, so 100 and 150 pass.

It is the same as writing >= 100 AND <= 150, but shorter and easier to read.

Try it: a range of prices

Code: sql
SELECT name, price_pence FROM products
WHERE price_pence BETWEEN 100 AND 150;

-- The same test written the long way:
SELECT name, price_pence FROM products
WHERE price_pence >= 100 AND price_pence <= 150;

AND, OR and NOT

WordKeeps a row whenExampleRows kept
`AND`both tests are true`category = 'drink' AND stock > 30`Cola, Water
`OR`one or both tests are true`category = 'fruit' OR category = 'snack'`Apple, Banana, Crisps, Cookie
`NOT`the test is false`NOT category = 'lunch'`Everything but lunch items

The cells students fill in are left blank in this preview.

Try it: join tests

Code: sql
SELECT name FROM products WHERE category = 'drink' AND stock > 30;

SELECT name FROM products WHERE category = 'fruit' OR category = 'snack';

SELECT name FROM products WHERE NOT category = 'lunch';

-- Change AND to OR in the first query. What changes?

Check

Have a go

Check

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.