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

Free lesson

OS 4.3 (10 of 12): What databases are used for

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A shop that never forgets

You buy a hoodie from an online shop. You log in. The shop knows your name. It takes the hoodie out of stock. It remembers your order. Next week it shows you a matching cap.

All of this is kept in a database: an organised store of data.

Key words

WordWhat it means
DatabaseAn organised store of data that programs can search and change.
TableA grid in a database. Each row is one record and each column is one fact.
QueryA question you ask a database.
SQLThe language used to ask a database questions and to change its data.
User managementKeeping track of who can log in and what each person may do.
StockHow many of each item you have to sell.
PersonalisationChanging what a page shows to fit one person.
DiagnosticsFinding out what went wrong, using records of what happened.
MillisecondOne thousandth of a second. 1,000 milliseconds is 1 second.
LogA list of events with the time they happened.

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

Read a query in plain words

You seeSay it in words
SELECT name, roleShow me the name and role columns.
FROM usersLook in the users table.
WHERE role = 'admin'Keep only the rows where the role is admin.
ORDER BY stockSort the rows, smallest first.
JOIN products ON ...Put two tables side by side where the ids match.
SUM(...), AVG(...)Add up a column, or find its average.
GROUP BY pageGive one answer for each page.
INSERT, UPDATEAdd a new row, or change a row.
-- some wordsA note for people. The database skips it.

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

Find who can log in as an admin

Code: sql
-- Who can log in as an admin?
SELECT name, role FROM users WHERE role = 'admin';

-- Change the query to list all customers.

Make a free account to save your work and track your progress.

Why not a text file?

A file is fine for ten names. For ten thousand customers, a database is much better.

A database finds one record fast. It stops two people changing the same data at once. It keeps the data safe if the power fails. It can answer many different questions about the same data.

Quick check

Have a go

Quick check

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

Four jobs for a database

JobWhat the database holdsExample question
User managementAccounts, roles and passwords (stored as hashes, never as plain text)Is this person an admin?
E-commerceProducts, stock, ordersHow many mugs are left?
DiagnosticsLogs of errors and eventsWhich page had errors yesterday?
Performance analysisHow long each page took to loadWhich page is slowest?

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

Find low stock and count orders

Code: sql
-- Which products have fewer than 5 left?
SELECT name, stock FROM products WHERE stock < 5;

-- How many mugs has each customer ordered?
SELECT users.name, SUM(orders.quantity)
FROM orders
JOIN users ON orders.user_id = users.user_id
WHERE orders.product_id = 2
GROUP BY users.name;

Make a free account to save your work and track your progress.

An order makes two changes

Code: sql
-- Ben buys one Cap (product 3). Two things must happen: record the order, and take one off the stock.
INSERT INTO orders VALUES (5, 2, 3, 1);
UPDATE products SET stock = stock - 1 WHERE product_id = 3;

-- How many caps are left? It was 2.
SELECT name, stock FROM products WHERE product_id = 3;

Make a free account to save your work and track your progress.

Past orders help suggest new things

Code: sql
-- What did Aisha buy? A page can use this to suggest similar things.
SELECT products.name
FROM orders
JOIN products ON orders.product_id = products.product_id
WHERE orders.user_id = 1;

Make a free account to save your work and track your progress.

Match each job to the data it needs

Have a go

Loading…

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

Find errors and slow pages

Code: sql
-- Diagnostics: which events were errors? (500 means error)
SELECT page, status FROM page_log WHERE status = 500;

-- Performance: the average load time of each page, in milliseconds
SELECT page, AVG(load_ms) FROM page_log GROUP BY page;

Make a free account to save your work and track your progress.

Extra: List the products to reorder

The shop owner wants a list of products to reorder. Write a query that shows the name and stock of every product with fewer than 5 in stock. Put the product with the least stock first.

Code: sql
SELECT * FROM products;

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

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

Exit quiz

Have a go

Exit quiz

5 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

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.