Cambridge IGCSE · Topic 9 (4)
IGCSE Computer Science 0478 / Topic 9 / IGCSE

Ask the table a question.

A games shop wants every racing game, cheapest first. Instead of reading the whole table, you write one short SQL query and the database returns exactly the rows and columns you asked for, in the order you asked for them.

The key idea

SQL (structured query language) is used to search a database. SELECT lists the fields to show. FROM names the table. WHERE gives a condition, so only the records that meet it are shown. ORDER BY sorts the results by a field, ASC (ascending: A to Z, smallest first) or DESC (descending: Z to A, largest first). To find the output, check every record against the WHERE condition, keep the matching ones, show only the SELECTed fields, then sort.

the four parts of a query SELECTTitle, Price FROMGames WHEREGenre = 'Racing' ORDER BYPrice DESC; which fields to show which table which records in what order SELECT and FROM are always needed; WHERE and ORDER BY are optional
Fig. 1: each line of a query does one job
Section 01

SELECT and FROM.

In the lab, build a query on the Books table: tick fields for SELECT and run it. Then add a condition and a sort and watch the result change. In this lesson every query uses the table GAMES below.

GameIDTitleGenrePriceYearOnline
G01Star RacerRacing25.002021TRUE
G02Castle QuestAdventure40.002019FALSE
G03Puzzle PopPuzzle10.002023TRUE
G04Speed KingsRacing35.002022TRUE
G05Dragon IsleAdventure30.002020FALSE
G06Block TownPuzzle15.002018FALSE
SELECT Title, Price
FROM Games;
Examiner trap

SELECT names fields, not records. "SELECT Racing" is wrong: the word Racing is a value in the Genre field, so it belongs in a WHERE condition.

Section 02

Choosing records with WHERE.

WHERE compares a field with a value. Only records for which the condition is true appear in the output. Text values go in quotes; numbers and TRUE/FALSE do not.

Operator=<><=>=<>
Meaningequal toless thangreater thanless than or equal togreater than or equal tonot equal to
Worked example Text condition
Give the output of this query on the GAMES table.
SELECT Title, Price
FROM Games
WHERE Genre = 'Racing';
  1. Check each record's Genre: G01 Racing (yes), G02 Adventure (no), G03 Puzzle (no), G04 Racing (yes), G05 Adventure (no), G06 Puzzle (no).
  2. Keep G01 and G04, and show only Title and Price.
Answer: Star Racer 25.00; Speed Kings 35.00
Worked example Number condition
Give the output of this query.
SELECT Title
FROM Games
WHERE Price >= 30;
  1. Prices: G01 25.00 (no), G02 40.00 (yes), G03 10.00 (no), G04 35.00 (yes), G05 30.00 (yes, 30 is equal), G06 15.00 (no).
  2. With no ORDER BY, the results come out in table order.
Answer: Castle Quest; Speed Kings; Dragon Isle
Your turn
Write a query to show the Title and Genre of every game that can be played online.
Examiner trap

Read < and <= carefully. WHERE Price > 30 would leave out Dragon Isle at exactly 30.00; WHERE Price >= 30 includes it.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which part of an SQL query decides which records appear in the output?

Quick check+10 XP

In the lab, the Books table is sorted with ORDER BY Year DESC. Which book is listed first?

Quick check+10 XP

How many records does SELECT Title FROM Games WHERE Price >= 30; return from the GAMES table?

Quick check+10 XP

Which keyword sorts the output from largest to smallest?

Section 03

Sorting with ORDER BY.

ORDER BY comes last. It sorts the output by one field, ASC (ascending) or DESC (descending). Try the lab's "what will it output?" questions: write your answer down before you reveal it.

SELECT Title, Year
FROM Games
WHERE Price < 30
ORDER BY Year ASC;
Worked example WHERE then ORDER BY
Give the output of the query above.
  1. Price < 30: G01 25.00 (yes), G02 40.00 (no), G03 10.00 (yes), G04 35.00 (no), G05 30.00 (no, 30 is not less than 30), G06 15.00 (yes).
  2. The matching records are Star Racer 2021, Puzzle Pop 2023 and Block Town 2018.
  3. Sort by Year, smallest first: 2018, 2021, 2023.
Answer: Block Town 2018; Star Racer 2021; Puzzle Pop 2023
Your turn
Give the output of: SELECT GameID, Title FROM Games WHERE Year > 2020 ORDER BY Title ASC;
Examiner trap

The keywords must be in this order: SELECT, FROM, WHERE, ORDER BY. And ORDER BY Price DESC sorts the output, it does not choose which records appear.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is the output of SELECT Title FROM Games WHERE Genre = 'Adventure' ORDER BY Year ASC; on the GAMES table?

Exam style+20 XP

Which query shows the title of every game that costs less than 20.00?

Exam style+20 XP

SELECT Title FROM Games WHERE Year <> 2021 ORDER BY Price DESC; Which title is listed last?

Exam style+20 XP

A query ends ORDER BY Surname; with no ASC or DESC. How is the output sorted?

Skill unlocked

SELECT, FROM, WHERE and ORDER BY, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Writing and completing SQL queries with SELECT, FROM, WHERE and ORDER BY, and working out their output.