GCSE Computer Science · public explanation

Files, Data Records and SQL

Follow data through files, records and database queries. Explore SQL selection and related tables with worked examples and GCSE OCR and AQA guidance.

Read the explanation and worked examples, then choose your exam board to continue with activities and practice in your account.

OCR J277On the specificationOCR J277 2.2.3

Apply files, data records and sql using OCR Exam Reference Language or a studied language only when requested.

AQA 8525 (2027+)On the specificationAQA 8525 (2027+) 3.2.6, 3.7.1–3.7.2

Apply files, data records and sql in the precise response format printed on the paper.

OCR focus

OCR: open, read, write and close

A text file retains an ordered collection of lines between program runs. A working variable holds temporary text in the current run. Opening an existing file provides a handle for file operations; closing releases that handle without deleting the stored lines. A new file must be created before it is opened.

In OCR Exam Reference Language, writeLine adds a line at the END of the file. It does not replace a named person's earlier record. readLine copies the next line into working state without deleting it, then advances the read position. endOfFile is a Boolean check: True means no unread line remains. Do not attempt another read after that result.

Trace file contents, working variables and the next read position separately. A clean new run starts with fresh temporary state while the file remains. If a question explicitly describes a different overwriting operation, follow that stated behaviour; do not assume OCR writeLine overwrites or that OCR open requires a Python-style mode argument.

Worked example: persist two scores

  1. The file initially contains exactly one text line: Mina,12. The comma and digits are stored text; line numbers are only a reading guide.
  2. Prepare Leo,15 in a working variable. Open the existing file and use writeLine to append it after Mina,12. Both stored lines remain in that order.
  3. Close the handle, start a clean new run and reopen. The first read gives Mina,12; the second replaces the working text with Leo,15. Neither read deletes a stored line.
  4. After both reads, endOfFile is True. There is no third line. Change only the working variable to Leo,99 without writing; the saved second line remains Leo,15.
  5. Close, restart and reopen again: the stored lines are still Mina,12 and Leo,15. In a separate reset comparison, an operation explicitly defined to clear the original file before writing Leo,15 would leave only that one line.

Appending, reading and assigning a working variable change different state. Only an appropriate file-writing operation changes the stored contents; closing does not clear an independently held variable.

AQA focus

AQA: relationships, primary keys and foreign keys

A database is an organised collection of data. A relational database stores related data in tables. A record is one row; a field is one named attribute or column, with a suitable data type. In this example, MemberID is an integer, while Name and Town contain text.

A primary key uniquely identifies a record in its table. A foreign key refers to a primary key in another table, linking the records. Two members can share the name Mina: MemberID 18 identifies the Mina in York, while MemberID 20 identifies a different Mina in Hull. Match identifiers, not names or row positions.

MemberID 18 can appear in several Loan records because one member can borrow several books. This useful repeated reference is different from copying her town into every loan. Storing the town once reduces redundancy and the risk of inconsistent copies when it changes. A database does not automatically repair a poorly chosen design.

TableExample recordKey and relationship
MemberMemberID 18; Name Mina; Town YorkMemberID is the primary key
MemberMemberID 19; Name Leo; Town BathA different member
MemberMemberID 20; Name Mina; Town HullSame name as member 18; different primary-key value
LoanLoanID 51; MemberID 18; BookName OrbitLoanID is the primary key; MemberID is a foreign key
LoanLoanID 52; MemberID 18; BookName TidesBoth loans link to member 18
LoanLoanID 53; MemberID 19; BookName SparkThis loan belongs to Leo

Worked example: member 18 moves from York to Leeds

  1. Change Town in member 18's record to Leeds. Keep her MemberID as 18.
  2. Leave both Loan records unchanged: their MemberID values still refer to 18.
  3. Look up either loan's borrower again. Both now recover Leeds from the same Member record.

One Member record changes; no Loan record changes. A lookup reads the relationship rather than copying a new town into each loan.

Select columns and filter rows

SELECT names the fields returned; FROM identifies the source table; WHERE tests which records qualify. A condition can test Score without returning Score. Filtering excludes rows from the result, not from the stored table. Write the clauses in SELECT, FROM, WHERE order; the reasoning trace is not a claim about a database engine's execution plan.

Check the exact boundary: >= 50 includes fifty, while > 50 excludes it. AND requires both conditions to be true for the same record; OR requires one or both. Write a text value such as 'North' in single quotes, while a numeric threshold such as 50 is unquoted.

SELECT Name, Score returns two fields; SELECT * returns every field. Without WHERE, every record qualifies. A condition matching no records produces zero result rows, not a blank record. Without ORDER BY the query does not guarantee result order; supplied-table order is used here only to present an inspectable trace.

EntryIDNameScoreTeam
11Mina72North
12Leo50South
13Sam49North
14Iris88South
15Ruth60North

Worked example: SELECT Name FROM Result WHERE Score >= 50

  1. Test each record's score: 72, 50, 88 and 60 qualify; 49 does not.
  2. Return only Name for those records: Mina, Leo, Iris and Ruth. Keep all five source records unchanged.
  3. Changing SELECT Name to SELECT Score returns their scores instead. Changing >= to > excludes Leo.
  4. Add AND Team = 'North': only Mina and Ruth qualify. Change AND to OR: all five qualify, because Sam meets the team test and Leo/Iris meet the score test.

The condition chooses records; SELECT chooses returned fields. A two-field result has two columns, independently of how many records qualify.

AQA focus

AQA: ordering and querying two linked tables

Use this separate library example with a new set of member and loan records. Member.MemberID names the MemberID field inside Member; Loan.MemberID names the borrower reference inside Loan. LoanID identifies a loan, not its borrower. AQA queries can retrieve data from up to two tables.

The equality Member.MemberID = Loan.MemberID links the right records. The separate condition Member.MemberID = 11 selects Mina. Both conditions must hold. Listing Member and Loan in FROM does not supply the missing equality.

ORDER BY arranges complete result records: ASC is ascending and DESC is descending. For these capitalised titles, ascending is alphabetic order. WHERE filters records; ORDER BY arranges those that qualify. Neither operation changes the stored tables. Without ORDER BY, do not assume a particular result order. Equal titles remain separate records; this ordering key alone does not specify their relative order.

TableRecordMeaning
MemberMemberID 11; Name MinaRequested member
MemberMemberID 12; Name LeoA different member
MemberMemberID 13; Name RuthA different member
LoanLoanID 71; MemberID 12; BookTitle TidesLeo's loan
LoanLoanID 72; MemberID 11; BookTitle OrbitMina's loan
LoanLoanID 73; MemberID 11; BookTitle AtlasMina's loan
LoanLoanID 74; MemberID 13; BookTitle ReefRuth's loan
LoanLoanID 75; MemberID 11; BookTitle TidesMina's loan

Worked example: return Mina's books in title order

  1. Use SELECT Member.Name, Loan.BookTitle FROM Member, Loan.
  2. Use WHERE Member.MemberID = Loan.MemberID AND Member.MemberID = 11. Loans 72, 73 and 75 qualify; loans 71 and 74 do not.
  3. Use ORDER BY Loan.BookTitle ASC. Return Mina with Atlas, then Mina with Orbit, then Mina with Tides. Her name appears once per matching loan.
  4. Change only ASC to DESC to return Tides, Orbit, Atlas. All three Member records and all five Loan records remain stored.

Without the key equality, the member filter alone would pair Mina with all five loans. Correct the relationship rather than deleting another person's stored loans.

Continue with your exam board

The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.

Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.