GCSE Computer Science · Topic guide

SQL and Data Handling

SQL and data handling covers how information is organised in databases and manipulated using Structured Query Language (SQL). It includes the difference between flat-file and relational databases, key database terms such as tables, records, fields, primary keys and foreign keys, and how to write SQL statements that select, insert, update and delete data. It is assessed in Paper 2 of GCSE Computer Science, and exam mark schemes expect SQL keywords such as SELECT, FROM, WHERE, INSERT INTO, UPDATE and DELETE to be written in upper case.

GCSE Grades 1-9Paper 2: Computational Thinking, Algorithms and ProgrammingOCR J277AQAEdexcelWJECEduqas

Before you start

Make sure you're comfortable with these topics first:

Method

  1. Learn the difference between a flat-file database, which stores all data in a single table, and a relational database, which splits data across multiple linked tables to reduce data redundancy.
  2. Learn key database vocabulary: a table stores records; a record is one row of related data; a field is one column/attribute; a primary key uniquely identifies each record in a table.
  3. Learn how a foreign key in one table stores the primary key value from another table, creating a relationship between the two tables and letting data be split without repeating it.
  4. Learn the structure of a SELECT statement: SELECT the field(s) needed, FROM the table, WHERE a condition filters which records are returned, always ending the statement with a semicolon.
  5. Practice combining WHERE conditions with AND and OR and the comparison operators (=, >, <, >=, <=, <>), and sorting results with ORDER BY ... ASC or DESC.
  6. Learn the syntax for the three data-manipulation statements: INSERT INTO ... VALUES (...) to add a record, UPDATE ... SET ... WHERE ... to change existing data, and DELETE FROM ... WHERE ... to remove a record.
  7. Write every SQL keyword in upper case (SELECT, FROM, WHERE, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM, ORDER BY) and copy field and table names exactly, since mark schemes credit this convention.
  8. Practice reading a small sample table and working out, by eye, exactly which records a given SQL statement would return, change or remove.

Worked example

A table called students stores: id | name | formGroup | mark, with the data: 1 | Amelia | 7A | 72, 2 | Noah | 7B | 55, 3 | Isla | 7A | 90, 4 | Leo | 7A | 58, 5 | Grace | 7B | 81. Write an SQL statement that selects the name and mark of every student in form group 7A with a mark greater than 60, sorted by mark from highest to lowest, then state which student(s) it would return and in what order.

  1. Identify the fields to return: name and mark, so start with SELECT name, mark.
  2. Identify the table the data comes from: FROM students.
  3. Identify the filter conditions, combined with AND since both must be true: WHERE formGroup = '7A' AND mark > 60.
  4. Identify the sort order: highest mark first means descending order, so ORDER BY mark DESC.
  5. Combine every clause in the correct order and end with a semicolon: SELECT name, mark FROM students WHERE formGroup = '7A' AND mark > 60 ORDER BY mark DESC;
  6. Check each record against the WHERE clause: Amelia (7A, 72) and Isla (7A, 90) both match; Leo (7A, 58) fails since 58 is not greater than 60, and Noah and Grace fail as their formGroup is not 7A, so the final answer returns Isla (90) then Amelia (72), in that order.

Practice questions

Try each question, then tap to reveal the answer.

Q1Define the term 'primary key' as used in a database table.Show answer

Answer: A field (or combination of fields) that uniquely identifies each record in a table, so no two records can share the same value.

Got it right?
Q2Give one advantage of a relational database over a flat-file database.Show answer

Answer: Splitting data across multiple linked tables reduces data redundancy (the same data being repeated), which also reduces the risk of data inconsistency when a value is updated.

Got it right?
Q3A table called books has fields bookId, title, authorId and copiesAvailable. Write an SQL statement that selects the title of every book with more than 2 copies available.Show answer

Answer: SELECT title FROM books WHERE copiesAvailable > 2;

Got it right?
Q4State what a foreign key is used for.Show answer

Answer: A field in one table that stores the primary key value from a record in another table, creating a link (relationship) between the two tables.

Got it right?
Q5A table called members has fields memberId, firstName, lastName and joinYear. Write an SQL statement that adds a new record with memberId 12, firstName 'Priya', lastName 'Shah' and joinYear 2026.Show answer

Answer: INSERT INTO members (memberId, firstName, lastName, joinYear) VALUES (12, 'Priya', 'Shah', 2026);

Got it right?
Q6A table called products has fields productId, name, price and stock. Write an SQL statement that changes the price of the product with productId 7 to 15.99.Show answer

Answer: UPDATE products SET price = 15.99 WHERE productId = 7;

Got it right?
Q7A table called bookings has fields bookingId, customerName and eventDate. Write an SQL statement that deletes the booking with bookingId 45.Show answer

Answer: DELETE FROM bookings WHERE bookingId = 45;

Got it right?
Q8A table called staff has fields staffId, name, department and salary, storing these records: staffId | name | department | salary, 1 | Ben | Sales | 24000, 2 | Chloe | IT | 31000, 3 | Dan | Sales | 27000. State which staffId(s) would be returned by: SELECT staffId FROM staff WHERE department = 'Sales' AND salary > 25000;Show answer

Answer: Only staffId 3 (Dan): Ben is in Sales but his salary of 24000 is not greater than 25000, and Chloe's department is IT, not Sales.

Got it right?

Exam-style questions

Written in the style of a GCSE Computer Science exam paper, with a full mark scheme.

Q1[3 marks]

A school database has a table called pupils with fields pupilId, name, tutorGroup and attendancePercent. Write an SQL statement that selects the name and attendancePercent of every pupil in tutor group '9C' whose attendancePercent is below 90, ordered from lowest attendance to highest.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 3 available

Got it right?
Q2[4 marks]

A gym stores all of its data in a single flat-file spreadsheet: one row per class booking, with the member's name, phone number and membership type repeated on every row that member has booked a class. (a) Identify one problem this flat-file design causes. (b) Explain how splitting the data into a members table and a bookings table, linked by a memberId foreign key, would solve this problem.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 4 available

Got it right?
Q3[6 marks]

A small dance school currently records every booking, along with the full name, phone number and membership type of the customer, on a single flat-file spreadsheet, with a new row added every time a customer books a class. Discuss the problems this flat-file approach could cause as the school grows, and evaluate whether moving to a relational database, split into a customers table and a bookings table linked by a customerId, would be worth the extra initial setup effort.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 6 available

Got it right?

See real GCSE Computer Science past-paper questions, with official mark schemes

Free printable worksheet

Want more practice on paper? Download the sql and data handling worksheet pack - 13 pages of exam-style questions with a full mark scheme. One email opens every download in this browser for 14 days - no account, no card. Print it for personal and classroom use.

Next topics

Ready to practise sql and data handling? Add it to a printable topic pack for this student in the Pack Builder.

Add to my pack

Not quite what you needed?

Tell us what is missing on sql and data handling, or which topic to write up next. Every request is read, and we reply to every one.

Build a full practice pack.

This topic is one of hundreds in the library - pick the ones a student needs and generate a printable PDF in minutes.