Skip to content
Book 6: Algorithms, Programming and Logic

Book 6 · Chapter 9

Databases

§9Core · ≈ 9.4 m · →B6·9

This chapter is in the ebook.

Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.

Open in the ebook

For the whole chapter

§9

Short code B6·9·1

Tables, records and fields

Short code B6·9·10

Output · 10 rows

BikeIDModelTypeFrameCmHourlyRateLastServiceElectricHires
BK01TrailblazerM483.52026-08-14FALSE22
BK02CityliteC522.252026-09-02FALSE35
BK03VoltraC545.752026-09-20TRUE41
BK04RidgebackM523.52026-07-30FALSE17
BK05SprintR564.252026-09-11FALSE9
BK06PipK301.52026-08-25FALSE12
BK07VoltraC505.752026-09-28TRUE38
BK08SummitM546.52026-06-18TRUE14
BK09CityliteC482.252026-09-05FALSE27
BK10PipK341.52026-07-12FALSE6
Run and checked before printing

Defining a table from requirements

Short code B6·9·11

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Primary keys

Short code B6·9·12

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

SQL: SELECT, FROM and WHERE

Short code B6·9·13

SELECT BikeID, Model
FROM BIKE
WHERE Type = 'M';
Only the mountain bikes (Type 'M'), and only the two fields named after SELECT, in that order.

Output · 3 rows

BikeIDModel
BK01Trailblazer
BK04Ridgeback
BK08Summit
Run and checked before printing
SELECT Model
FROM BIKE
WHERE Type = M;
A text value without its quotes. The engine reads M as the name of a field.

Output

ERROR: M is not a column — text values must be in quotes: Type = 'M'.
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Hires > '20';
A number in quotes. The query runs, but the engine flags it: quotes make 20 into text.

Output

WARNING: '20' is text but Hires is INTEGER — write the number without quotes: Hires > 20.
Run and checked before printing

Output · 4 rows

BikeIDModel
BK05Sprint
BK06Pip
BK08Summit
BK10Pip
Run and checked before printing
SELECT Model, FrameCm
FROM BIKE
WHERE FrameCm >= 52;
Predict the result, then turn the page.
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Electric;
A Boolean field on its own is already TRUE or FALSE, so WHERE Electric means the same as WHERE Electric = TRUE.

Output

BikeID
BK03
BK07
BK08
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Model = Pip;
Check for the exhibit test, condition (a): a text value without quotes (meant to fail).

Output

ERROR: Pip is not a column — text values must be in quotes: Model = 'Pip'.
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Electric = 'TRUE';
Check for the exhibit test, condition (c): TRUE in quotes is text, not a Boolean.

Output

ERROR: Electric is BOOLEAN — write Electric = TRUE (no quotes).
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Model = 'Citylite';
Check for the margin question on WHERE: the bikes whose model is Citylite.

Output

BikeID
BK02
BK09
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE FrameCm = 52;
Check for the margin question on WHERE: the bikes with a frame of exactly 52 cm.

Output

BikeID
BK02
BK04
Run and checked before printing

AND and OR

Short code B6·9·14

SELECT Model, FrameCm
FROM BIKE
WHERE FrameCm >= 52;
The prediction from the last page, run for real. Notice that the two 52 cm bikes are in: >= includes 52.

Output · 5 rows

ModelFrameCm
Citylite52
Voltra54
Ridgeback52
Sprint56
Summit54
Run and checked before printing
SELECT BikeID, Model, Hires
FROM BIKE
WHERE Type = 'C' AND Electric = FALSE;
AND: a row is kept only when both conditions are TRUE. Only the two Citylites are city bikes without a motor.

Output · 2 rows

BikeIDModelHires
BK02Citylite35
BK09Citylite27
Run and checked before printing
SELECT BikeID, Model, HourlyRate
FROM BIKE
WHERE Type = 'R' OR HourlyRate >= 5.75;
OR: a row is kept when at least one condition is TRUE. The Sprint is a road bike; the other three cost 5.75 or more.

Output · 4 rows

BikeIDModelHourlyRate
BK03Voltra5.75
BK05Sprint4.25
BK07Voltra5.75
BK08Summit6.5
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Model = 'Pip' OR 'Voltra';
Half a condition after OR.

Output

ERROR: OR needs a complete condition on each side — write Model = 'Voltra', not just 'Voltra': … Model = 'Pip' OR Model = 'Voltra'.
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Type = 'C' OR Type = 'M' AND Electric = TRUE;
AND and OR mixed with no brackets. The query runs, but the engine warns that it may not mean what you think.

Output

WARNING: AND is evaluated before OR, so this reads as Type = 'C' OR (Type = 'M' AND Electric = TRUE) — if you meant something else, add brackets to show which conditions belong together.
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE (Type = 'C' OR Type = 'M') AND Electric = TRUE;
With brackets: electric bikes that are city or mountain bikes. The two non-electric Citylites are now left out.

Output

BikeID
BK03
BK07
BK08
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Type = 'C' OR HourlyRate > 6;
Check for the exhibit test: city bikes, or bikes that cost more than 6.00 an hour.

Output

BikeID
BK02
BK03
BK07
BK08
BK09
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Electric = TRUE AND HourlyRate < 6;
Check for the margin question on AND: electric bikes that cost less than 6.00 an hour.

Output

BikeID
BK03
BK07
Run and checked before printing

Sorting with ORDER BY

Short code B6·9·15

SELECT Model, Hires
FROM BIKE
WHERE Type = 'C'
ORDER BY Hires DESC;
City bikes, most-hired first. DESC puts the largest value at the top.

Output · 4 rows

ModelHires
Voltra41
Voltra38
Citylite35
Citylite27
Run and checked before printing
SELECT BikeID, LastService
FROM BIKE
WHERE LastService < '2026-08-01'
ORDER BY LastService ASC;
Dates sort from earliest to latest with ASC. These three bikes were last serviced before 1 August.

Output · 3 rows

BikeIDLastService
BK082026-06-18
BK102026-07-12
BK042026-07-30
Run and checked before printing
SELECT BikeID
FROM BIKE
WHERE Type = 'M'
ORDER BY Hires;
Sorted by a field that is not shown: Hires 14, 17 and 22 decide the order, but only BikeID is output.

Output

BikeID
BK08
BK04
BK01
Run and checked before printing
SELECT BikeID, Model
FROM BIKE
WHERE Type = 'K'
ORDER BY Hires DESC;
Check for the margin question on ORDER BY: the kids' bikes, most hires first.

Output · 2 rows

BikeIDModel
BK06Pip
BK10Pip
Run and checked before printing

Counting and totalling: COUNT and SUM

Short code B6·9·16

SELECT COUNT(*)
FROM BIKE
WHERE Electric = TRUE;
COUNT gives one value: how many records meet the condition.

Output

COUNT(*)
3
Run and checked before printing
SELECT SUM(Hires)
FROM BIKE;
SUM adds up a number field over every record: all the hires this month.

Output

SUM(Hires)
221
Run and checked before printing
SELECT SUM(Hires)
FROM BIKE
WHERE Type = 'C';
SUM with a WHERE adds up only the records that pass: 35 + 41 + 38 + 27.

Output

SUM(Hires)
141
Run and checked before printing
SELECT COUNT(Hires)
FROM BIKE
WHERE Type = 'K';
COUNT on a number field still counts records. The two kids' bikes have 12 and 6 hires, but the answer is 2.

Output

COUNT(Hires)
2
Run and checked before printing
SELECT SUM(Hires)
FROM BIKE
WHERE Type = 'M';
Check for the margin question on SUM: the total hires of the mountain bikes.

Output

SUM(Hires)
53
Run and checked before printing
SELECT COUNT(*)
FROM BIKE
WHERE Type = 'M';
Check for the margin question on COUNT: how many mountain bikes there are.

Output

COUNT(*)
3
Run and checked before printing

What does this query output?

Short code B6·9·17

SELECT Model, Hires
FROM BIKE
WHERE Type = 'C' AND Hires < 40
ORDER BY Hires;
The Mark Ladder's query, run: three rows, smallest Hires first.

Output · 3 rows

ModelHires
Citylite27
Citylite35
Voltra38
Run and checked before printing
SELECT BikeID, Hires
FROM BIKE
WHERE Electric = FALSE AND HourlyRate > 3
ORDER BY Hires DESC;
The faded twin's query, run.

Output · 3 rows

BikeIDHires
BK0122
BK0417
BK059
Run and checked before printing
SELECT BikeID, Model
FROM BIKE
WHERE Electric = TRUE AND Hires > 20
ORDER BY Hires DESC;
The completed statement from the Mark Ladder, run: the two electric Voltras.

Output · 2 rows

BikeIDModel
BK03Voltra
BK07Voltra
Run and checked before printing
SELECT BikeID, Modle
FROM BIKE;
One letter out of place in a field name.

Output

ERROR: Unknown column Modle — did you mean Model?
Run and checked before printing
SELECT Model, HourlyRate
FROM BIKE
WHERE Type = 'M' OR Type = 'R'
ORDER BY HourlyRate ASC;
Check for the faded twin: mountain and road bikes, cheapest first.

Output · 4 rows

ModelHourlyRate
Trailblazer3.5
Ridgeback3.5
Sprint4.25
Summit6.5
Run and checked before printing

Command-word coach

Short code B6·9·18

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook: Databases

Short code W6·9 · Algorithms, Programming and Logic Workbook

The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Starter files

Short code W6·9·K

Data files and skeleton code in Pseudocode, Python, VB.NET, Java for this chapter's tasks arrive here with the chapter.

Ask Z about Databases
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook