SQL

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Programming fundamentals › Additional programming techniques

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 11 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

SQL (Structured Query Language) is used to search a database table and return the records that meet a condition. You need SELECT, FROM and WHERE, with comparison operators, AND, OR and the * wildcard. Expect 'write an SQL statement' and 'state the output of this query' questions based on a table.

Key facts

  • SELECT fields (comma-separated), or * for all fields
  • FROM the table name
  • WHERE the condition a record must meet
  • Operators: = > < >= <= ; combine conditions with AND / OR
  • Text values in quotation marks; numbers without
  • Record = row; field = column
  • Pattern: SELECT Title, Year FROM Films WHERE Year > 2020

Notes

The parts of an SQL query

  • SELECT lists the fields to show, separated by commas. SELECT * shows all fields.
  • FROM names the table to search.
  • WHERE gives the condition a record must meet to be included.
  • Conditions use = , > , < , >= and <=, and can be combined with AND and OR.
  • Text values go in quotation marks (Genre = 'Drama'); numbers do not (Year > 2020).

Worked example

  • Table Films:
    FilmIDTitleGenreYearMinutes
    F01Nova HarbourSci-fi2019128
    F02Quiet TidesComedy202194
    F03Deep SignalSci-fi2023141
    F04Salt RoadDrama2020110
  • SELECT Title, Year FROM Films WHERE Genre = 'Sci-fi' AND Minutes > 130 returns one record: Deep Signal, 2023. (Nova Harbour is Sci-fi but only 128 minutes.)
  • SELECT * FROM Films WHERE Minutes >= 128 returns all the fields of F01 and F03.
  • The table has 4 records and 5 fields.

Records and fields

diagram
  • Each row of a table is a record (all the data about one film).
  • Each column is a field (one piece of data, such as Year).
  • FilmIDTitleGenreYearMinutesF01Nova HarbourSci-fi2019128F02Quiet TidesComedy202194F03Deep SignalSci-fi2023141F04Salt RoadDrama2020110one recordone field
    A row is a record; a column is a field
  • Field names in a query must match the table exactly.

How to answer each type of question

State the output of a query

2 to 3 marksGrade 5
  1. Check each record against the WHERE condition, one at a time.
  2. For AND both parts must be true; for OR either part.
  3. Give only the fields listed after SELECT, for every matching record.

Example. Using the Films table in the notes above, state the output of:
SELECT Title FROM Films WHERE Genre = 'Sci-fi' OR Year > 2020 [2]

Show the model answerHide the model answer
Nova Harbour and Deep Signal (1); Quiet Tides (1), with no other titles. (Salt Road is Drama and from 2020, which is not greater than 2020.)

Write an SQL query

3 marksGrade 6
  1. SELECT the fields asked for (or * for all).
  2. FROM the table named in the question.
  3. WHERE the condition, using exact field names, quotation marks for text and the right operator.

Example. Using the Films table in the notes above, write an SQL statement to display the title and year of every film that is shorter than 120 minutes. [3]

Show the model answerHide the model answer
SELECT Title, Year (1) FROM Films (1) WHERE Minutes < 120
(1)
(This returns Quiet Tides, 2021 and Salt Road, 2020.)

Identify records and fields

1 to 2 marksGrade 4
  1. Count the rows of data (not the heading row) for records.
  2. Count the columns for fields.

Example. Using the Films table in the notes above, state the number of records and the number of fields. [2]

Show the model answerHide the model answer
4 records (1); 5 fields (1).

Write a query with two conditions

3 to 4 marksGrade 7
  1. Decide whether both conditions must be true (AND) or either one is enough (OR).
  2. Write each condition in full, with its own field name.
  3. Check your query against the table to make sure it returns the right records.

Example. Using the Films table in the notes above, write an SQL statement to display the FilmID and Title of every Sci-fi film made after 2020. [4]

Show the model answerHide the model answer
SELECT FilmID, Title (1) FROM Films (1) WHERE Genre = 'Sci-fi' (1) AND Year > 2020
(1)
(This returns F03, Deep Signal.)

Don’t lose marks

  • Missing the commas between field names after SELECT.
  • Leaving the quotation marks off text values, or putting them round numbers.
  • Writing WHERE Genre = 'Comedy' OR 'Drama' instead of repeating the field name.
  • Using AND when a record can only meet one of the conditions (a film cannot be Comedy and Drama at once).
  • Getting > and >= wrong at the boundary value.

More tips

Memory tricks

  • SFW: Select (what), From (where), Where (which ones).
  • * means 'every field', not 'every record': WHERE still filters the records.
  • Check each record against the condition one row at a time; tick the matches.

Exam technique

  • Copy field and table names exactly as they appear in the question.
  • Write SELECT, FROM and WHERE in capitals so each part is clear.
  • In output questions, list only the fields in the SELECT line, in that order.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 4
    Identify records and fields in a tableEach row is a record; each column is a field.
  2. Grade 5
    State the output of a simple queryPick out the fields named after SELECT for every record that meets the WHERE condition.
  3. Grade 5
    Write a query using SELECT, FROM and WHEREChoose the fields, the table and a condition such as Minutes < 120.
  4. Grade 6
    Use * to select all fieldsSELECT * returns every field of each matching record.
  5. Grade 7
    Combine conditions with AND and ORFor example WHERE Genre = 'Sci-fi' AND Minutes > 130.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

A music website stores its songs in a table called Tracks. This statement is run:
SELECT Title, Length
FROM Tracks
WHERE Artist = 'Nova Blue'
State the name of the field that is used in the condition.
Artist
SELECT is followed by the names of the fields to display. Is this statement true or false?
True
Name the SQL keyword used to search for records that match a pattern, such as all names that start with ‘M’.
LIKE
Conditions in the WHERE part of an SQL statement use comparison operators. State what > means in a condition.
Greater than

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy2 marks
State the purpose of the WHERE part of an SQL statement.
[2]
Show the answer and mark scheme
Answer: It sets the condition (criteria) a record must meet, so only the matching records are displayed.
  • to set a condition / criteria (that records must meet)
  • so that only the records that match the condition are displayed / selected
Question 2Medium2 marks
A student wrote this SQL statement to display the name and number of medals of every athlete with more than 5 medals, from a table called Athletes.
SELECT Name Medals
FORM Athletes
WHERE Medals > 5
(a) The statement has two mistakes. Identify and correct the first mistake, which is in the SELECT line.
[1]
(b) Identify and correct the second mistake.
[1]
Show the answer and mark scheme
(a) Answer: SELECT Name, Medals
  • comma missing between Name and Medals (SELECT Name, Medals)
(b) Answer: FROM
  • FORM should be FROM
Question 3Hard7 marks
A car dealer stores details of cars for sale in a table called Cars with the fields Reg, Make, Colour, Year, Mileage and Price.
(a) Write an SQL statement to display all the fields for cars made from 2018 to 2021 inclusive.
[3]
(b) Write an SQL statement to display the registration and price of every car that is red or blue and has a mileage of less than 40 000.
[4]
Show the answer and mark scheme
(a) Answer:
SELECT * FROM Cars
WHERE Year >= 2018 AND Year <= 2021
  • SELECT * FROM Cars
  • WHERE Year >= 2018
  • AND Year <= 2021
(b) Answer:
SELECT Reg, Price FROM Cars
WHERE (Colour = 'Red' OR Colour = 'Blue')
AND Mileage < 40000
  • SELECT Reg, Price FROM Cars
  • Colour = 'Red' OR Colour = 'Blue'
  • AND Mileage < 40000
  • brackets around the OR condition so that the mileage condition applies to both colours

More in Additional programming techniques

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.