GCSE Computer Science

GCSE SQL: queries and worked examples

Read a small database table, write a query and check exactly which records it returns.

The starting table: Books

These invented titles and numbers are a practice dataset, not library records. Use this same table for every example.

A field is one category of data, such as Pages, shown as a column. A record is one complete row: book 101 has the title Orbit, genre SciFi and 180 pages. This table has four fields and five records. BookID is the primary key: its value uniquely identifies each book.

A relational database can contain related tables. We use only one table here so you can concentrate on reading queries; this is not a complete guide to relational database design.

BookIDTitleGenrePages

101

Orbit

SciFi

180

102

Tide

Adventure

120

103

Signal

SciFi

240

104

Forest

Adventure

180

105

Comet

SciFi

90

1. Choose fields with SELECT and FROM

SELECT Title, Genre
FROM Books;

SELECT Title, Genre requests two columns, in that order. FROM Books names the source table. There is no filter, so all five records contribute to the result. The commas separate field names.

Without ORDER BY, SQL does not guarantee the order of the returned rows. This table shows the rows in one possible order; the selected values are what matter. The query reads data without changing the Books table.

TitleGenre

Orbit

SciFi

Tide

Adventure

Signal

SciFi

Forest

Adventure

Comet

SciFi

2. Filter records with WHERE

SELECT Title, Pages
FROM Books
WHERE Pages >= 180;

Read the condition as “Pages is greater than or equal to 180”. Orbit and Forest are included because equality counts; Signal is included because 240 is greater. Tide and Comet are excluded.

WHERE selects which records qualify; SELECT still decides which fields appear. A result does not automatically include every field used in its condition. Again, row order is not guaranteed.

TitlePages

Orbit

180

Signal

240

Forest

180

Comparison operators you will meet

Put text values in straight single quotation marks, such as 'SciFi'. Numeric values such as 180 do not need quotation marks. Keep the field names exactly as supplied in the question. These examples use SQL = for equality, rather than Python ==.

OperatorMeaningExample condition

=

Equal to

Genre = 'SciFi'

<>

Not equal to

Genre <> 'SciFi'

>

Greater than

Pages > 180

<

Less than

Pages < 180

>=

Greater than or equal to

Pages >= 180

<=

Less than or equal to

Pages <= 180

3. Sort results with ORDER BY

SELECT Title, Pages
FROM Books
WHERE Pages >= 180
ORDER BY Title ASC;

This keeps the same three books as example 2 and sorts them by title from A to Z. ASC means ascending; DESC means descending. Sorting changes the order of the result, not which books satisfy the condition.

Write the clauses in the order shown: SELECT, FROM, WHERE, then ORDER BY. For OCR J277, this sorting example extends beyond the SQL keywords listed in the specification; for AQA 8525, ORDER BY is listed content.

TitlePages

Forest

180

Orbit

180

Signal

240

4. Require both conditions with AND

SELECT Title, Pages
FROM Books
WHERE Genre = 'SciFi'
  AND Pages >= 180;

A book must be SciFi and have at least 180 pages. Orbit and Signal satisfy both conditions. Comet satisfies the genre condition but is too short. Forest has enough pages but the wrong genre. The displayed row order is one possible order.

TitlePages

Orbit

180

Signal

240

5. Accept either condition with OR

SELECT Title, Pages
FROM Books
WHERE Genre = 'Adventure'
  OR Pages < 100;

A book qualifies if it is Adventure or has fewer than 100 pages, or both. Tide and Forest qualify by genre; Comet qualifies by length. Each qualifying record appears once. The displayed row order is one possible order.

Write a complete comparison on each side of AND or OR. For example, use WHERE Title = 'Orbit' followed by OR Title = 'Comet'. Repeat Title = for each comparison.

TitlePages

Tide

120

Forest

180

Comet

90

Practice answer: SciFi books, longest first

SELECT Title, Pages
FROM Books
WHERE Genre = 'SciFi'
  AND Pages <= 180
ORDER BY Pages DESC;

Orbit and Comet qualify. Signal is SciFi but exceeds 180 pages. Tide and Forest fail the genre condition. DESC puts 180 before 90. Changing <= to < would incorrectly remove Orbit: “no more than” includes the boundary value.

Compare your answer in three ways: the selected columns, the surviving records and their requested order. A query can run successfully and still answer the wrong question.

TitlePages

Orbit

180

Comet

90

Related Ed Centre pages

These linked pages help students and parents move between closely related guidance instead of reaching a dead end.

Section overview

Subject guidance for students

Clear guides to what different subjects involve, the skills they build and practical ways to feel more confident in lessons.

Related guide

GCSE English Language vs English Literature: what’s the difference?

In England and Northern Ireland they are normally separate GCSEs; learners beginning Year 10 in Wales from September 2025 normally take an integrated award, while Scotland mainly uses National 5 English. Compare what each assesses, how papers vary and how to revise effectively.

Support and clarity

Frequently asked questions

Straight answers to the questions people ask most often.

Does a SELECT query delete records that do not match?

No. WHERE filters the returned result. Records that do not match remain in the underlying table; the SELECT queries in this guide do not update or delete data.

Why can two results contain the same books in a different order?

Without ORDER BY, row order is not guaranteed. Compare the returned values rather than expecting the order of the original table. If the question requires a particular order, include the appropriate ORDER BY clause.

Sources and references

Sources and references