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: Asking for rows with SELECT and WHERE

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

Ten thousand rows, one answer

The school shop sells hundreds of items a week. The manager wants to know which products are nearly sold out.

Reading every row by eye takes hours. One short line of code gives the answer in a blink.

Key words

WordWhat it meansExample
Databasean organised store of tables that you can ask questions
SQLthe language used to ask a database for dataSELECT name FROM products;
Queryone question written in SQL
Tablea grid of data: rows go across and columns go down
Columnone field in every row, such as price
SELECTthe SQL word that says which columns to show
FROMthe SQL word that says which table to read
WHEREthe SQL word that keeps only the rows that pass a test

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

By the end of this lesson

Note

I can write a query with SELECT, FROM and WHERE to get exactly the rows and columns I need.

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.

Try it: read a whole table

Code: sql
-- The star means every column.
SELECT * FROM products;

-- Now ask for only two columns. Remove the two dashes to run it.
-- SELECT name, price_pence FROM products;

The shape of a query

A query has clauses in a fixed order. Write SELECT first, then FROM, then WHERE.

SELECT lists the columns. FROM names the table. WHERE keeps only the rows that pass a test. End the query with a semicolon.

The tests you can write in WHERE

TestMeaningExampleRows kept
`=`equal to`category = 'drink'`Cola, Orange juice, Water
`<>`not equal to`category <> 'drink'`Everything but the drinks
`<`less than`stock < 10`Banana, Cookie, Chicken wrap, Cheese toastie
`>`greater than`price_pence > 200`Cheese roll, Chicken wrap, Cheese toastie
`<=`less than or equal`stock <= 8`Chicken wrap, Cookie, Banana, Cheese toastie
`>=`greater than or equal`price_pence >= 250`Cheese roll, Chicken wrap, Cheese toastie

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

Try it: keep only some rows

Code: sql
SELECT name, stock FROM products WHERE stock < 10;

-- Text goes in single quotes.
SELECT name, price_pence FROM products WHERE category = 'lunch';

-- Change the number 10 and run the first query again.

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

5 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.