AQA GCSE CSPaper 2 · Unit 7Lesson 6

Paper 2 · Unit 7 · CS-L7-06

SQL: Wildcards with LIKE

60 minutes · AQA 8525 · Paper 2 — Relational databases & SQL

Spec & Goals 3 min

AQA Spec 3.7.2 — Pattern matching with LIKE and wildcards

By the end of this lesson you can:

  1. Use LIKE to match a pattern instead of an exact value.
  2. Use the wildcard % (any characters) and _ (one character).
  3. Predict which records a LIKE pattern returns.

Warm-Up 5 min

WHERE Town = 'Penang' needs an exact match. But what if you want every town starting with "P", or every name containing "ai"? That needs pattern matching.

Quick starter

How would you ask for all students whose name begins with the letter A?

Reveal the idea

Match the pattern "A followed by anything": WHERE Name LIKE 'A%'. The % stands for any characters.

Key Concept — matching patterns 14 min

LIKE is used in a WHERE clause to match a pattern in text, using wildcards.

WildcardMatchesPatternExample matches
%any number of characters (incl. none)'P%'Penang, Perak, P
%ends with…'%a'Aisyah ✗, Melaka ✓ (ends "a")
%contains…'%ai%'Aisyah, Cairo
_exactly one character'10_'10A, 10B (not 10AB)

Our Student table (reminder)

NameFormTown
Aisyah10AIpoh
Wei Jie10BPenang
Priya10AIpoh
Arjun11CJohor Bahru
Mei Ling11CPenang
Hafiz10BKuala Lumpur

Worked Example — patterns in action 12 min

Query 1: students whose Town starts with "P".

SELECT Name, Town
FROM Student
WHERE Town LIKE 'P%';
NameTown
Wei JiePenang
Mei LingPenang

Query 2: students whose Name contains "i".

SELECT Name
FROM Student
WHERE Name LIKE '%i%';
Name
Aisyah
Wei Jie
Priya
Mei Ling
Hafiz

Query 3: forms in year 10 (pattern 10 + one character).

SELECT DISTINCT Form
FROM Student
WHERE Form LIKE '10_';

Matches 10A and 10B (not 11C).

Try It Yourself 12 min

🟢 Easy

Goal: Write a query for students whose Name starts with "A".

🟡 Medium

Goal: Write a query for towns ending in "h", showing Name and Town.

Hint: '%h'.

🔴 Stretch

Goal: Explain the difference between LIKE 'A%', LIKE '%A' and LIKE '_A%'.

📝 Exam Practice 10 min

State[2 marks]

State what the wildcards % and _ mean in a LIKE pattern.

Mark scheme
  • % — any number of characters (including none) (1).
  • _ — exactly one character (1).
Write a program[3 marks]

Write a query to display the Name of every student whose name contains the letters "ei".

Mark scheme
  • SELECT Name FROM Student (1).
  • WHERE Name LIKE (1).
  • '%ei%'; — correct wildcards both sides (1).
State[1 mark]

State the result of SELECT Name FROM Student WHERE Town LIKE 'I%';

Mark scheme
  • Aisyah and Priya (both from Ipoh) (1).

Recap & Key Terms 3 min

LIKE matches a text pattern using wildcards: % stands for any characters, _ for exactly one. Placement sets the meaning — 'A%' starts with A, '%A' ends with A, '%A%' contains A.

LIKE
An SQL operator that matches a pattern of characters using wildcards.
% (wildcard)
Matches any number of characters, including none.
_ (wildcard)
Matches exactly one character.

Homework 1 min

Task (≤ 15 min): For a Book(Title, Author) table, write queries for: (a) titles starting with "The"; (b) authors containing "lee"; (c) titles exactly four characters long.

Model answer
-- (a)
SELECT Title FROM Book WHERE Title LIKE 'The%';

-- (b)
SELECT Author FROM Book WHERE Author LIKE '%lee%';

-- (c)
SELECT Title FROM Book WHERE Title LIKE '____';

Award marks for: correct starts-with pattern (1); correct contains pattern (1); four underscores for four characters (1).