Skip to content
Book 8: Algorithms, Programming and Databases

Book 8 · Chapter 11

Databases and SQL

§11.1§11.2Exam craftB8·11

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

§11.1 Databases

Short code B8·11·1

§11.2 SQL

Short code B8·11·2

Tables, records and fields

Short code B8·11·10

Output · 8 rows

FilmIDTitleGenreMinutesCertificateRating
F01The Last LighthouseDrama11812A4.4
F02Robo PalsAnimation92U4.1
F03Desert RunAction131153.8
F04Moonlit MysteriesMystery105PG4.6
F05Snow Leopard TrailDocumentary88U4.7
F06Space CadetsAnimation97PG3.9
F07Monsoon HeartsDrama124PG4.2
F08Night MarketMystery11212A3.6
Run and checked before printing

Data types

Short code B8·11·11

Output · 12 rows

ScreeningIDFilmIDShowDateStartTimeScreenPriceSubtitlesSeatsSold
S101F022026-11-0614:00A5FALSE64
S102F012026-11-0617:30B7.5FALSE41
S103F032026-11-0620:15A8FALSE88
S104F052026-11-0711:00B5TRUE37
S105F022026-11-0714:00A5FALSE92
S106F042026-11-0717:30A7.5TRUE75
S107F072026-11-0720:15B8TRUE58
S108F032026-11-0814:00B6.5FALSE49
S109F062026-11-0811:00A5FALSE70
S110F042026-11-0820:15B8FALSE46
S111F012026-11-0817:30A7.5TRUE52
S112F072026-11-0815:45B7.5FALSE33
Run and checked before printing

Defining a single-table database

Short code B8·11·12

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

Primary keys

Short code B8·11·13

INSERT INTO FILM VALUES ('F04', 'Paper Kites', 'Family', 101, 'U', 4.0);
Someone tries to add a new film with the FilmID F04, which Moonlit Mysteries already has. The primary key refuses it.

Output

ERROR: Duplicate primary key: FILM already has a row with FilmID = 'F04'. A primary key must be unique — use a different FilmID (the highest so far is 'F08').
Run and checked before printing

Two tables and foreign keys

Short code B8·11·14

INSERT INTO SCREENING VALUES ('S113', 'F09', '2026-11-09', '14:00', 'A', 5.00, FALSE, 0);
A screening of film F09 is added, but there is no film F09. The foreign key refuses it.

Output

ERROR: FOREIGN KEY violation: FilmID = 'F09' does not exist in FILM (did you mean 'F01'?). Referential integrity — a SCREENING row can only refer to a FILM row that exists, so insert the FILM row first.
Run and checked before printing

Joining two tables

Short code B8·11·15

SELECT SCREENING.ScreeningID, SCREENING.FilmID, FILM.Title, SCREENING.StartTime
FROM SCREENING
INNER JOIN FILM ON SCREENING.FilmID = FILM.FilmID;
The two tables joined: each screening is matched with the FILM record that has the same FilmID. Notice that Night Market (F08) is missing. It has no screening.

Output · 12 rows

ScreeningIDFilmIDTitleStartTime
S101F02Robo Pals14:00
S102F01The Last Lighthouse17:30
S103F03Desert Run20:15
S104F05Snow Leopard Trail11:00
S105F02Robo Pals14:00
S106F04Moonlit Mysteries17:30
S107F07Monsoon Hearts20:15
S108F03Desert Run14:00
S109F06Space Cadets11:00
S110F04Moonlit Mysteries20:15
S111F01The Last Lighthouse17:30
S112F07Monsoon Hearts15:45
Run and checked before printing

SELECT, FROM and WHERE

Short code B8·11·16

SELECT Title, Minutes
FROM FILM
WHERE Minutes > 110;
A number condition: no quotes round 110.

Output · 4 rows

TitleMinutes
The Last Lighthouse118
Desert Run131
Monsoon Hearts124
Night Market112
Run and checked before printing
SELECT FilmID, Title
FROM FILM
WHERE Genre = 'Animation';
A text condition: the value 'Animation' is in quotes.

Output · 2 rows

FilmIDTitle
F02Robo Pals
F06Space Cadets
Run and checked before printing
SELECT ScreeningID, FilmID, ShowDate
FROM SCREENING
WHERE Subtitles = TRUE;
A Boolean condition: TRUE, with no quotes.

Output · 4 rows

ScreeningIDFilmIDShowDate
S104F052026-11-07
S106F042026-11-07
S107F072026-11-07
S111F012026-11-08
Run and checked before printing
SELECT ScreeningID, StartTime
FROM SCREENING
WHERE ShowDate = '2026-11-07';
A date condition and a time condition: dates and times are written in quotes.

Output · 4 rows

ScreeningIDStartTime
S10411:00
S10514:00
S10617:30
S10720:15
Run and checked before printing
SELECT Title, Rating
FROM FILM
WHERE Rating >= 4.2;
The query traced on the next page: which films are rated 4.2 or more?

Output · 4 rows

TitleRating
The Last Lighthouse4.4
Moonlit Mysteries4.6
Snow Leopard Trail4.7
Monsoon Hearts4.2
Run and checked before printing

AND and OR

Short code B8·11·17

SELECT Title, Minutes
FROM FILM
WHERE Genre = 'Drama' AND Minutes < 120;
AND: both conditions must be true.

Output · 1 row

TitleMinutes
The Last Lighthouse118
Run and checked before printing
SELECT Title, Certificate
FROM FILM
WHERE Certificate = 'U' OR Certificate = 'PG';
OR: either condition may be true. The field is named in both conditions.

Output · 5 rows

TitleCertificate
Robo PalsU
Moonlit MysteriesPG
Snow Leopard TrailU
Space CadetsPG
Monsoon HeartsPG
Run and checked before printing
SELECT Title, Certificate
FROM FILM
WHERE Certificate = 'U' OR 'PG';
The same query with the second field left out. The engine refuses it.

Output

ERROR: OR needs a complete condition on each side — write Certificate = 'PG', not just 'PG': … Certificate = 'U' OR Certificate = 'PG'.
Run and checked before printing
SELECT ScreeningID, Screen, SeatsSold
FROM SCREENING
WHERE Price = 8.00 AND (Subtitles = TRUE OR SeatsSold > 80);
AND with OR: the brackets say which conditions belong together.

Output · 2 rows

ScreeningIDScreenSeatsSold
S103A88
S107B58
Run and checked before printing
SELECT ScreeningID, SeatsSold
FROM SCREENING
WHERE Screen = 'A' AND SeatsSold > 70;
Check run for the AND worked example: Screen A, more than 70 seats

Output · 3 rows

ScreeningIDSeatsSold
S10388
S10592
S10675
Run and checked before printing
SELECT ScreeningID, Price
FROM SCREENING
WHERE Price = 5.00 OR Subtitles = TRUE;
Check run for the faded twin: 5.00 or subtitles

Output · 7 rows

ScreeningIDPrice
S1015
S1045
S1055
S1067.5
S1078
S1095
S1117.5
Run and checked before printing

ORDER BY

Short code B8·11·18

SELECT Title, Rating
FROM FILM
ORDER BY Rating DESC;
ORDER BY … DESC: the highest rating first.

Output · 8 rows

TitleRating
Snow Leopard Trail4.7
Moonlit Mysteries4.6
The Last Lighthouse4.4
Monsoon Hearts4.2
Robo Pals4.1
Space Cadets3.9
Desert Run3.8
Night Market3.6
Run and checked before printing
SELECT ScreeningID, StartTime
FROM SCREENING
WHERE ShowDate = '2026-11-08'
ORDER BY StartTime ASC;
WHERE first, then ORDER BY … ASC: Sunday's screenings, earliest first.

Output · 5 rows

ScreeningIDStartTime
S10911:00
S10814:00
S11215:45
S11117:30
S11020:15
Run and checked before printing

SUM and COUNT

Short code B8·11·19

SELECT SUM(SeatsSold)
FROM SCREENING
WHERE ShowDate = '2026-11-07';
SUM adds up the values in a field: the seats sold on Saturday.

Output

SUM(SeatsSold)
262
Run and checked before printing
SELECT COUNT(*)
FROM SCREENING
WHERE Price = 5.00;
COUNT counts the records that pass the WHERE: how many screenings cost 5.00?

Output

COUNT(*)
4
Run and checked before printing
SELECT COUNT(FilmID)
FROM FILM
WHERE Genre = 'Mystery';
COUNT with a field name gives the same answer here: two films are mysteries.

Output

COUNT(FilmID)
2
Run and checked before printing
SELECT COUNT(SeatsSold)
FROM SCREENING
WHERE ShowDate = '2026-11-06';
Fault 6: it runs, but the answer is wrong. The task was the total seats sold on Friday; COUNT gives the number of screenings.

Output

COUNT(SeatsSold)
3
Run and checked before printing
SELECT SUM(SeatsSold)
FROM SCREENING
WHERE ShowDate = '2026-11-06';
Fault 6 corrected: SUM adds the seats.

Output

SUM(SeatsSold)
193
Run and checked before printing
SELECT SUM(SeatsSold)
FROM SCREENING
WHERE Subtitles = TRUE;
Check run for the SUM worked example: seats sold at subtitled screenings

Output

SUM(SeatsSold)
222
Run and checked before printing
SELECT COUNT(*)
FROM SCREENING
WHERE ShowDate = '2026-11-07';
Check run for the COUNT faded twin: screenings on Saturday

Output

COUNT(*)
4
Run and checked before printing

INSERT INTO

Short code B8·11·20

INSERT INTO FILM
VALUES ('F09', 'Paper Kites', 'Family', 101, 'U', 4.0);

SELECT * FROM FILM WHERE FilmID = 'F09';
INSERT INTO without a field list: a value for every field, in the table's order. The SELECT after it shows the new record.

Output · 1 row

FilmIDTitleGenreMinutesCertificateRating
F09Paper KitesFamily101U4
Run and checked before printing
INSERT INTO SCREENING (ScreeningID, FilmID, ShowDate, StartTime, Screen, Price, Subtitles, SeatsSold)
VALUES ('S113', 'F08', '2026-11-09', '19:00', 'A', 6.50, FALSE, 0);

SELECT ScreeningID, FilmID, ShowDate, StartTime FROM SCREENING WHERE ShowDate = '2026-11-09';
INSERT INTO with a field list: the values match the fields one by one.

Output · 1 row

ScreeningIDFilmIDShowDateStartTime
S113F082026-11-0919:00
Run and checked before printing

INNER JOIN … ON

Short code B8·11·21

SELECT FILM.Title, SCREENING.StartTime
FROM FILM
INNER JOIN SCREENING ON FILM.FilmID = SCREENING.FilmID
WHERE SCREENING.ShowDate = '2026-11-07'
ORDER BY SCREENING.StartTime ASC;
INNER JOIN with WHERE and ORDER BY: the title and start time of every Saturday screening, in time order.

Output · 4 rows

TitleStartTime
Snow Leopard Trail11:00
Robo Pals14:00
Moonlit Mysteries17:30
Monsoon Hearts20:15
Run and checked before printing
SELECT Title, StartTime
FROM FILM
INNER JOIN SCREENING ON FILM.FilmID = SCREENING.FilmID
WHERE FilmID = 'F02';
FilmID is a field of both tables, so on its own it is ambiguous.

Output

ERROR: FilmID is ambiguous — it is a column of FILM and SCREENING. Say which one: FILM.FilmID or SCREENING.FilmID.
Run and checked before printing
SELECT SUM(SCREENING.SeatsSold)
FROM SCREENING
INNER JOIN FILM ON SCREENING.FilmID = FILM.FilmID
WHERE FILM.Genre = 'Animation';
SUM across two tables: the seats sold for animated films. Genre is in FILM; SeatsSold is in SCREENING.

Output

SUM(SCREENING.SeatsSold)
226
Run and checked before printing
SELECT COUNT(*)
FROM SCREENING
INNER JOIN FILM ON SCREENING.FilmID = FILM.FilmID
WHERE FILM.Certificate = 'PG';
COUNT across two tables: how many screenings are of PG films?

Output

COUNT(*)
5
Run and checked before printing
SELECT FILM.Title, SCREENING.ScreeningID
FROM FILM
INNER JOIN SCREENING ON FILM.FilmID = SCREENING.FilmID
WHERE FILM.Genre = 'Mystery';
Mystery films and their screenings. Night Market is a mystery, but it has no screening, so the INNER JOIN leaves it out.

Output · 2 rows

TitleScreeningID
Moonlit MysteriesS106
Moonlit MysteriesS110
Run and checked before printing
SELECT FILM.Title, SCREENING.Price
FROM FILM
INNER JOIN SCREENING ON FILM.FilmID = SCREENING.FilmID
WHERE SCREENING.Screen = 'B';
Check run for the join worked example: Screen B

Output · 6 rows

TitlePrice
The Last Lighthouse7.5
Desert Run6.5
Moonlit Mysteries8
Snow Leopard Trail5
Monsoon Hearts8
Monsoon Hearts7.5
Run and checked before printing
SELECT FILM.Title, SCREENING.ShowDate
FROM FILM
INNER JOIN SCREENING ON FILM.FilmID = SCREENING.FilmID
WHERE FILM.Genre = 'Drama';
Check run for the join faded twin: Drama screenings

Output · 4 rows

TitleShowDate
The Last Lighthouse2026-11-06
The Last Lighthouse2026-11-08
Monsoon Hearts2026-11-07
Monsoon Hearts2026-11-08
Run and checked before printing

Correcting SQL statements

Short code B8·11·22

SELECT Title, Genre
FILM
WHERE Minutes < 100;
Fault 1: FROM is missing.

Output

ERROR: WHERE comes after FROM <table> — the query needs FROM to say which table to read.
Run and checked before printing
SELECT Title
FROM FILM
WHERE Genre = Drama;
Fault 2: a text value without quotes.

Output

ERROR: Drama is not a column — text values must be in quotes: Genre = 'Drama'.
Run and checked before printing
SELECT Title
FROM FILM
ORDER BY Title
WHERE Rating > 4;
Fault 3: the clauses in the wrong order.

Output

ERROR: WHERE must come before ORDER BY — the order is SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT.
Run and checked before printing
SELECT Title
FROM FILM
ORDER Minutes DESC;
Fault 4: BY is missing after ORDER.

Output

ERROR: ORDER must be followed by BY: ORDER BY <column>, but found 'Minutes'.
Run and checked before printing
SELECT Title
FROM FILMS
WHERE Minutes < 100;
Fault 5: the table name misspelt.

Output

ERROR: There is no table called FILMS. Did you mean FILM? The database has: FILM.
Run and checked before printing
SELECT FILM.Title, SCREENING.StartTime
FROM FILM
INNER JOIN SCREENING ON FILM.Title = SCREENING.ScreeningID;
Fault 7: the tables joined on the wrong fields. The engine warns, and no rows come back.

Output

WARNING: ON FILM.Title = SCREENING.ScreeningID does not use the key that links FILM and SCREENING — the foreign key is SCREENING.FilmID → FILM.FilmID. Did you mean ON SCREENING.FilmID = FILM.FilmID?; No rows: the JOIN found no matching pairs — check that the ON condition pairs the foreign key with the primary key it references.
Run and checked before printing

Explaining the result of a statement

Short code B8·11·23

SELECT Title, Minutes
FROM FILM
WHERE Genre = 'Animation' OR Minutes > 125
ORDER BY Minutes DESC;
Check run for the output worked example

Output · 3 rows

TitleMinutes
Desert Run131
Space Cadets97
Robo Pals92
Run and checked before printing
SELECT ScreeningID
FROM SCREENING
WHERE ShowDate = '2026-11-06'
ORDER BY SeatsSold ASC;
Check run for the output faded twin

Output

ScreeningID
S102
S101
S103
Run and checked before printing

Command-word coach

Short code B8·11·24

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

Workbook: Databases and SQL

Short code W8·11 · Algorithms, Programming and Databases 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 W8·11·K

Data files and skeleton code in Python for this chapter's tasks arrive here with the chapter.

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