AQA GCSE CSPaper 2 · Unit 7Lesson 5

Paper 2 · Unit 7 · CS-L7-05

SQL: WHERE & Logical Operators

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

Spec & Goals 3 min

AQA Spec 3.7.2 — Filtering records with WHERE; AND, OR and comparison operators

By the end of this lesson you can:

  1. Use WHERE to return only records that meet a condition.
  2. Use comparison operators (=, >, <, >=, <=, <>).
  3. Combine conditions with AND and OR.

Warm-Up 5 min

SELECT chose the columns; now WHERE chooses the rows — returning only the records that match a condition.

Quick starter

To list only students in form 10A, what test must each row pass?

Reveal the idea

The Form field must equal '10A'. In SQL: WHERE Form = '10A'. Text values go in single quotes.

Key Concept — choosing rows 14 min

SELECT fields
FROM table
WHERE condition;

Only records where the condition is true are returned.

Comparison operators

OperatorMeansExample
=equal toForm = '10A'
> / <greater / less thanMark > 80
>= / <=greater/less than or equalMark >= 65
<>not equal toTown <> 'Ipoh'

Combining conditions with AND / OR

  • ANDboth conditions must be true.
  • OReither condition may be true.

Reminder of the Student table:

StudentIDNameFormTownMark
101Aisyah10AIpoh78
102Wei Jie10BPenang65
103Priya10AIpoh91
104Arjun11CJohor Bahru54
105Mei Ling11CPenang88
106Hafiz10BKuala Lumpur72

Worked Example — filter the students 12 min

Query 1: students who scored more than 80.

SELECT Name, Mark
FROM Student
WHERE Mark > 80;
NameMark
Priya91
Mei Ling88

Query 2: students in form 10A and from Ipoh.

SELECT Name
FROM Student
WHERE Form = '10A' AND Town = 'Ipoh';
Name
Aisyah
Priya

Query 3: students from Penang or Kuala Lumpur.

SELECT Name, Town
FROM Student
WHERE Town = 'Penang' OR Town = 'Kuala Lumpur';
NameTown
Wei JiePenang
Mei LingPenang
HafizKuala Lumpur

Try It Yourself 12 min

🟢 Easy

Goal: Write a query for the names of all students in form 11C.

🟡 Medium

Goal: Write a query for students with a Mark of at least 70, showing Name and Mark.

🔴 Stretch

Goal: Write a query for students in form 10B who scored below 70. State the result rows.

📝 Exam Practice 10 min

Write a program[3 marks]

Write an SQL query to display the Name of every student whose Town is Penang.

Mark scheme
  • SELECT Name (1).
  • FROM Student (1).
  • WHERE Town = 'Penang'; — correct field, quotes and value (1).
Write a program[2 marks]

Write a query to list students in form 10A with a Mark over 75.

Mark scheme
  • SELECT ... FROM Student WHERE Form = '10A' (1).
  • AND Mark > 75; (1).
State[1 mark]

State the result of SELECT Name FROM Student WHERE Mark < 60;

Mark scheme
  • Arjun (only student with Mark below 60) (1).

Recap & Key Terms 3 min

WHERE filters rows by a condition. Use comparison operators (= > < >= <= <>), quoting text and dates but not numbers. Combine conditions with AND (both true) and OR (either true).

WHERE
The SQL clause that returns only records meeting a condition.
Comparison operators
=, >, <, >=, <=, <> — test a field against a value.
AND
Combines conditions so both must be true.
OR
Combines conditions so either may be true.

Homework 1 min

Task (≤ 15 min): Using the Student table, write queries for: (a) students not from Ipoh; (b) students in form 10A or 10B; (c) students with a mark between 70 and 90 (inclusive).

Model answer
-- (a)
SELECT Name FROM Student WHERE Town <> 'Ipoh';

-- (b)
SELECT Name FROM Student WHERE Form = '10A' OR Form = '10B';

-- (c)
SELECT Name, Mark FROM Student WHERE Mark >= 70 AND Mark <= 90;

Award marks for: correct use of <> (1); correct OR (1); correct AND range (1).