SQL and Data Handling - Worksheets, Questions and Revision

17 original exam-style questions - 9 pages of questions with a full mark scheme - free printable PDF.

Download PDFJump to mark scheme (page 10)
« Previous: Programming Practice: Writing and Refining ProgramsNext: Trace Tables and Binary/Hex Conversions »
Revision Library
revisionlibrary.co.uk
GCSE · Computer Science

P2.8 SQL and Data Handling

OCR J277 · Calculators not allowed · about 85 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
A librarian at Chesterfield Library keeps records about members and the books they borrow, stored electronically instead of on paper.
(a)Which term describes a single item of data stored about one attribute (e.g. just a member's Town)?(1)
  • A) Record
  • B) Field
  • C) Table
  • D) Database
(b)Which term describes all the data stored about one entity (e.g. all the data about one member)?(1)
  • A) Record
  • B) Field
  • C) Table
  • D) Database
(c)Which term describes a collection of related records, each made up of the same set of fields?(1)
  • A) Record
  • B) Field
  • C) Table
  • D) Query
(d)Which term describes an organised collection of structured data, typically made up of one or more related tables?(1)
  • A) Record
  • B) Field
  • C) Table
  • D) Database
(Total for Question 1 is 4 marks)
2
The librarian's Members table stores data about every person who has joined the library.
MemberIDFirstNameLastNameTownJoinYearBooksOut
1001AishaKhanPreston20232
1002BenOgunleyePreston20220
1003ChloeWrightKendal20245
1004DmitriNovakKendal20231
1005ElinPryceLancaster20213
1006FrankieOseiPreston20240
1007GraceTaylorLancaster20224
1008HarunMalikKendal20232
1009IslaFernandesLancaster20240
1010JamieConnollyPreston20211
(a)State the number of fields in the Members table.(1)
(b)State the number of records in the Members table.(1)
(c)Identify the most suitable field to use as the primary key for this table, and give one reason for your choice.(2)
(Total for Question 2 is 4 marks)
3
Using the Members table from question 2, answer the following by searching the data manually (without writing any SQL).
(a)State the FirstName of the member whose MemberID is 1005.(1)
(b)State how many members live in the town of Preston.(1)
(c)State how many members currently have 0 books out (BooksOut = 0).(1)
(d)List the MemberIDs of all members who joined in 2023 (JoinYear = 2023).(1)
(Total for Question 3 is 4 marks)
4
Before switching to a database, the library kept each member's details on a separate paper form filed in a cabinet. Explain why storing this data in a database instead makes it easier to find information about members.
(Total for Question 4 is 3 marks)
5
The library is designing the Members table and needs to choose an appropriate data type for each field. For each field below, state the most appropriate data type, choosing from: text (string), integer, real (decimal), Boolean.
(a)MemberID(1)
(b)JoinYear(1)
(c)Town(1)
(d)A new field called HasPaid, which records whether a member has paid their annual membership fee.(1)
(Total for Question 5 is 4 marks)
6
SQL (Structured Query Language) can be used to search a database table. The statement SELECT * FROM tablename; displays every field of every record; SELECT field1, field2 FROM tablename; displays only the named fields. Using the Members table from question 2, write the following SQL statements.
(a)Write an SQL statement to display every field, for every record, in the Members table.(2)
(b)Write an SQL statement to display only the FirstName and LastName fields, for every record, in the Members table.(2)
(Total for Question 6 is 4 marks)
7
The keyword WHERE lets an SQL statement filter which records are returned, based on a condition. Using the Members table from question 2, write the following SQL statements.
(a)Write an SQL statement to display the FirstName, LastName and Town fields for every member who lives in Lancaster or Kendal.(2)
(b)Write an SQL statement to display all fields for every member who has more than 2 books currently out (BooksOut > 2).(2)
(Total for Question 7 is 4 marks)
8
State the FirstName and Town that would be output when the following SQL statement is run on the Members table from question 2:
SELECT FirstName, Town FROM Members WHERE JoinYear = 2024;
(Total for Question 8 is 3 marks)
9
Using the Members table from question 2, write the following SQL statements, each combining a WHERE condition with the AND or OR keyword.
(a)Write an SQL statement to display the FirstName and LastName of every member who lives in Preston AND currently has 0 books out.(2)
(b)Write an SQL statement to display all fields for every member who lives in Lancaster OR has more than 3 books currently out (BooksOut > 3).(3)
(Total for Question 9 is 5 marks)
10
The keyword ORDER BY sorts the output of an SQL statement by a chosen field; ASC sorts ascending (smallest/earliest first, and is the default if omitted) and DESC sorts descending (largest first). Using the Members table from question 2, write the following SQL statements.
(a)Write an SQL statement to display the FirstName, LastName and BooksOut fields, for every member, sorted so that the member with the most books out appears first.(3)
(b)Write an SQL statement to display the LastName field, for every member, sorted alphabetically from A to Z.(2)
(Total for Question 10 is 5 marks)
11
Write a single SQL statement, using the Members table from question 2, to display the FirstName, LastName and JoinYear fields for every member who joined before 2023 (JoinYear < 2023), sorted so that the earliest JoinYear appears first.
(Total for Question 11 is 4 marks)
12
The keyword LIKE, combined with the wildcard %, matches any sequence of characters within a text field (for example, 'A%' matches any value starting with A). Using the Members table from question 2, write the following SQL statements.
(a)Write an SQL statement to display all fields for every member whose LastName begins with the letter O.(2)
(b)A librarian wants to find every member whose Town contains the letters "cas" anywhere within it (for example, to find Lancaster). Write an SQL statement to display the FirstName and Town of these members.(2)
(Total for Question 12 is 4 marks)
13
The library is adding a new field, EmailAddress, to the Members table when a new member joins.
(a)Explain how a presence check could be used to validate data entered into the EmailAddress field.(2)
(b)Explain how a length check could also be used to validate data entered into the EmailAddress field.(1)
(Total for Question 13 is 3 marks)
14
The library also keeps a Loans table, which records each time a book is borrowed:
LoanIDMemberIDBookTitleDueDate
50011001The Hobbit21/07/2026
50021003Wuthering Heights18/07/2026
50031003Great Expectations25/07/2026
50041007198420/07/2026
(a)State the primary key of the Loans table.(1)
(b)The MemberID field in the Loans table is a foreign key. Explain what is meant by a foreign key, referring to the Members and Loans tables in your answer.(2)
(c)State one benefit of storing member details in a separate Members table, rather than repeating FirstName, LastName and Town directly inside the Loans table every time a book is borrowed.(2)
(Total for Question 14 is 5 marks)
15
A small library currently stores all its data in a single flat-file table, with columns MemberID, FirstName, LastName, Town, BookTitle and DueDate, adding a new row every time a member borrows a book (so a member who borrows three books appears in three separate rows, with their FirstName, LastName and Town repeated each time).

Discuss the problems this flat-file approach could cause, and explain how splitting the data into two related tables, as in the Members and Loans tables in question 14, solves these problems.
(Total for Question 15 is 6 marks)
16
A librarian writes the following SQL statement to search the Members table from question 2:
SELECT FirstName FROM Members WHERE Town = 'Preston' AND BooksOut = 0 OR Town = 'Kendal';

In SQL, AND is evaluated before OR when both appear in the same WHERE clause (unless brackets are used to change the order). State the FirstNames that this statement would output, listing every member that meets the condition.
(Total for Question 16 is 3 marks)
17
Write a single SQL statement, using the Members table from question 2, that would display the FirstName, LastName and JoinYear fields for every member whose LastName contains the letter sequence "an" and who joined in 2023 or later (JoinYear ≥ 2023), sorted with the earliest JoinYear first.
(Total for Question 17 is 5 marks)
Mark scheme · P2.8 SQL and Data Handling

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12

Question 13

Question 14

Question 15

Question 16

Question 17