A-level Computer Science revision · Exchanging data

SQL queries

The idea

SQL retrieves data with SELECT fields FROM table WHERE condition ORDER BY field. LIKE with % matches patterns. JOIN (or INNER JOIN ... ON) combines tables using matching keys. AND, OR, BETWEEN and IN refine conditions.

  1. SELECT the fields; FROM the table(s).
  2. WHERE filters rows; ORDER BY sorts (ASC or DESC).
  3. JOIN tables ON matching key fields.

Watch out In LIKE patterns, % stands for any number of characters.

See it worked

Worked example

Write SQL to list the names of students in Year 12, sorted alphabetically.

  1. SELECT Name FROM Student
  2. WHERE Year = 12
  3. ORDER BY Name ASC

Answer SELECT Name FROM Student WHERE Year = 12 ORDER BY Name ASC

Your turn

Question 11 mark

Which condition finds surnames beginning with S?

Choose one
Question 21 mark

What does ORDER BY Price DESC do?

Choose one
Question 32 marks

Write an SQL query that lists the Title of every Book whose Price is between 5 and 10.

Write your answer, then see a model answer
  1. SELECT and FROM.
  2. WHERE with BETWEEN.

Answer SELECT Title FROM Book WHERE Price BETWEEN 5 AND 10

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms