Skip to the page
Year 10 · Computer Science · Summer 2 · Databases and the Year 10 mock

SQL: SELECT, FROM, WHERE, ORDER BY

PracticalPresent ▶

What this lesson covers

9.3 SQL: SELECT, FROM, WHERE, AND, OR

Cambridge IGCSE Computer Science 0478 · Topic 9: Databases · Paper 2

9.4 SQL: ORDER BY, SUM and COUNT

Cambridge IGCSE Computer Science 0478 · Topic 9: Databases · Paper 2

You will learn to

  • Explain what SELECT, FROM and WHERE each do in a query.
  • Write WHERE conditions with comparison operators, AND and OR.
  • Sort the results with ORDER BY, using ASC or DESC.
  • Predict the exact output of a given query.

Success criteria

  • I can write ten past-paper queries by hand.
  • I can predict which records and fields a query will show.
  • I can sort the output by one field, highest first.
The syllabus points for these sections
  • Explain what each part of a SELECT query does.
  • Use comparison operators in a WHERE condition.
  • Combine conditions with AND and OR.
  • Predict the exact output of a query.
  • Sort query output with ORDER BY ASC and DESC.
  • Total a numeric field with SUM.
  • Count records with COUNT.
  • Complete and write full queries for exam scenarios.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Database
An organised, structured store of data that a computer can search and sort.
Table
A grid of data made of rows (records) and columns (fields).
Field
One column of a table; it holds one item of data about every record, such as Minutes.
Record
One row of a table; it holds all the data about one thing, such as one bike hire.
Field name
The heading at the top of a column, such as HireID; it is not a record.
Data type
The kind of data a field can hold, such as integer, real or Boolean.
Text / alphanumeric
A data type for letters, digits and symbols, such as a name or a phone number.
Integer
A data type for whole numbers, such as 55.
Real
A data type for numbers with a decimal part, such as 5.50.
Date/time
A data type for a date, a time, or both, such as 05/03/2026.
SQL
Structured Query Language: the language used to ask a database for data.
Query
A question to the database, written in SQL, that returns matching data.
ORDER BY
The SQL clause that sorts the output, ascending (ASC) or descending (DESC).
SUM
An SQL function that adds up the values in a numeric field.
COUNT
An SQL function that counts how many records meet a condition.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

9.3 SQL: SELECT, FROM, WHERE, AND, OR

The shape of a query

SQL (Structured Query Language) is the language used to ask a database for data. A question written in SQL is called a query.

`SELECT` lists the fields you want to see. `FROM` names the table. `WHERE` gives the condition a record must meet.

The output is a new, smaller table. It has only the chosen fields, and only the records that meet the condition.

SymbolMeaningExample
=equal toStation = 'Sol'
<>not equal toBikeType <> 'E'
>greater thanMinutes > 30
<less thanCost < 1.00
>=greater than or equal toMinutes >= 30
<=less than or equal toCost <= 2.00
SELECT HireID, Station
FROM BikeHire
WHERE Minutes > 30;
?Why?

Text values go inside single quotes, like `'Sol'`, so SQL knows where the text starts and ends. Numbers and TRUE/FALSE have no quotes.

Predicting output: one condition

Use the same three moves every time. First, filter: go down the table and tick each record that meets the WHERE condition.

Second, pick columns: keep only the fields named after SELECT, in the order they are listed.

Third, sort, if there is an ORDER BY. With no ORDER BY, keep the records in table order.

HireIDMinutes
H0240
H0555
H0842
e.g.Example
Filter: Minutes > 30 H01 25 ✗ · H02 40 ✓ · H03 12 ✗ · H04 18 ✗ H05 55 ✓ · H06 30 ✗ · H07 8 ✗ · H08 42 ✓ Pick columns HireID and Minutes. The output is in the table below.
SELECT HireID, Minutes
FROM BikeHire
WHERE Minutes > 30;
!Watch out

H06 has exactly 30 minutes. 30 is not greater than 30, so H06 is not in the output. Read `>` and `>=` very carefully.

AND: both conditions must be true

AND joins two conditions. A record is shown only if both conditions are true.

AND makes the output smaller, because each record has to pass two tests.

HireIDCost
H012.50
H084.20
e.g.Example
Records at Sol: H01 (E), H04 (M), H08 (E) H01: Sol ✓ and E ✓ → shown H04: Sol ✓ and M ✗ → not shown H08: Sol ✓ and E ✓ → shown
SELECT HireID, Cost
FROM BikeHire
WHERE Station = 'Sol' AND BikeType = 'E';

OR: at least one condition must be true

OR joins two conditions. A record is shown if either condition is true, or both.

OR makes the output bigger, because a record has two chances to pass.

Write the full condition on both sides of OR. The field name must appear twice.

HireIDStation
H02Retiro
H03Atocha
H06Retiro
H07Atocha
SELECT HireID, Station
FROM BikeHire
WHERE Station = 'Retiro' OR Station = 'Atocha';
!Watch out

`WHERE Station = 'Retiro' OR 'Atocha'` is wrong and loses the mark. Each side of OR must be a complete condition: `Station = 'Atocha'`.

9.4 SQL: ORDER BY, SUM and COUNT

ORDER BY: sorting the output

ORDER BY sorts the output by one field. It always comes last in the query.

ASC means ascending: smallest to largest, A to Z, oldest date to newest.

DESC means descending: largest to smallest, Z to A, newest date to oldest.

HireIDMinutes
H0240
H0418
H078
e.g.Example
Filter BikeType = 'M': H02 (40), H04 (18), H07 (8) Sort by Minutes, largest first: 40, 18, 8
SELECT HireID, Minutes
FROM BikeHire
WHERE BikeType = 'M'
ORDER BY Minutes DESC;
?Why?

If you leave out ASC or DESC, the database sorts in ascending order. Writing ASC anyway makes your answer clear to the examiner.

SUM: adding up a field

SUM adds up all the values in a numeric field, for the records that meet the condition.

The output is one single number, not a list of records.

Only use SUM on integer or real fields. You cannot add up text.

e.g.Example
Members (Member = TRUE): H01, H03, H04, H06 Costs: 2.50 + 1.20 + 0.90 + 3.00 2.50 + 1.20 = 3.70 3.70 + 0.90 = 4.60 4.60 + 3.00 = 7.60 Output: 7.60
SELECT SUM(Cost)
FROM BikeHire
WHERE Member = TRUE;
!Watch out

Put the field name inside the brackets: `SUM(Cost)`. Writing `SUM Cost` or `SUM(Member)` will not earn the mark.

COUNT: how many records match

COUNT counts how many records meet the condition. It does not add the values.

The output is one single whole number.

Here we write `COUNT(HireID)`. You may also see `COUNT(*)`, which counts every matching record in the same way.

e.g.Example
Electric bikes: H01, H03, H05, H06, H08 Count them: 1, 2, 3, 4, 5 Output: 5
SELECT COUNT(HireID)
FROM BikeHire
WHERE BikeType = 'E';
?Why?

SUM answers "how much?". COUNT answers "how many?". Read the question for those words to choose the right one.

Putting it all together

A full query can use WHERE with AND, then ORDER BY. Work through it with the three moves: filter, pick columns, sort.

Then test yourself in the toy below. Its BikeHire table has different fields: BikeID, Type, Station, Hours and Cost. Type a query, predict the output on paper first, then press Run query.

Try the example buttons, then change `ASC` to `DESC`, or `AND` to `OR`. Watch how the output changes.

StationMinutesCost
Moncloa555.50
Retiro402.00
Sol424.20
e.g.Example
Filter: non-members (H02, H05, H07, H08) whose cost is 2.00 or more H02 2.00 ✓ · H05 5.50 ✓ · H07 0.40 ✗ · H08 4.20 ✓ Pick columns: Station, Minutes, Cost Sort by Station A to Z: Moncloa, Retiro, Sol
SELECT Station, Minutes, Cost
FROM BikeHire
WHERE Cost >= 2.00 AND Member = FALSE
ORDER BY Station ASC;

There is something to try for this step in the IGCSE library.

Writing a query from a question

Exam questions describe what is needed in words. Turn each part of the sentence into one line of SQL.

"Show the hire ID and station" → SELECT. "from the bike-hire table" → FROM. "of hires under 20 minutes or from Moncloa" → WHERE. "highest hire ID first" → ORDER BY … DESC.

Copy field names and the table name exactly as they appear in the table.

HireIDStation
H07Atocha
H05Moncloa
H04Sol
H03Atocha
SELECT HireID, Station
FROM BikeHire
WHERE Minutes < 20 OR Station = 'Moncloa'
ORDER BY HireID DESC;
!Watch out

A query that mixes AND and OR can be read two ways. SQL does AND first. If you must mix them, put brackets around the part that should be done first.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Try it

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.