Be able to perform searches using a variety of operators including AND, OR, NOT, LIKE, >, <, =, >=, <=
18 Databases – Performing Searches with Operators
Learning Objective
By the end of this lesson you will be able to:
search a database using the operators AND, OR, NOT, LIKE, >, <, =, >=, <=;
understand how searching fits into the wider database workflow (design, manipulation, presentation);
recognise common pitfalls such as operator precedence, data‑type mismatches and syntax errors;
apply basic e‑safety and data‑protection principles when querying personal data.
1. Why Searches Matter – The Wider Database Context
Database design first: searches are performed on tables that have been created with appropriate field types (text, number, date, Boolean) and keys (primary/foreign). A well‑designed structure ensures that the correct operators can be used.
Presenting the results (Section 18.3): after a search you may generate a report, add headings/footers, or export the data. Only the fields required for the task should be displayed.
e‑Safety (Section 8.2/8.3): when personal or sensitive data are involved, retrieve only what is needed, keep the query secure and store results safely.
2. Operator Overview
Operator
Purpose
Typical use in a WHERE clause
AND
Both conditions must be true
WHERE price > 20 AND stock > 0
OR
Either condition can be true
WHERE category = 'Books' OR category = 'Magazines'
NOT
Negates a condition
WHERE NOT status = 'Discontinued'
LIKE
Pattern matching with wild‑cards (text fields only)
WHERE name LIKE 'A%'
>
Greater than (numeric or date fields)
WHERE quantity > 50
<
Less than (numeric or date fields)
WHERE quantity < 10
=
Equal to (any field type)
WHERE country = 'UK'
>=
Greater than or equal to
WHERE score >= 80
<=
Less than or equal to
WHERE score <= 50
3. Operator Precedence
When several operators appear in the same WHERE clause, SQL evaluates them in this order (the default precedence), unless parentheses are used to override it:
1️⃣ NOT 2️⃣ AND 3️⃣ OR
Always use parentheses to make the intended logic explicit and to avoid accidental results.
Example – Correct use of parentheses
WHERE (category = 'Electronics' AND price < 100) OR category = 'Books'
Without the parentheses the query would be interpreted as:
WHERE category = 'Electronics' AND (price < 100 OR category = 'Books')
which returns a different set of records.
4. Using the Operators in Practice
4.1 Combining AND, OR and NOT
AND + OR: group the AND part(s) with parentheses.
WHERE (status = 'Active' AND region = 'East') OR region = 'West'
NOT with other operators: enclose the whole condition you wish to exclude.
WHERE NOT (category = 'Books' OR category = 'Magazines')
4.2 Pattern Matching with LIKE (text fields only)
Wild‑cards:
% – any sequence of characters (including none)
_ – exactly one character
Examples:
Names that start with “J”: WHERE name LIKE 'J%'
E‑mail addresses that end in “.edu”: WHERE email LIKE '%.edu'
Postcodes with a letter followed by two digits: WHERE postcode LIKE '_##'
Note: Only % and _ are required for the IGCSE exam; other pattern syntax (e.g., []) is not needed.
4.3 Numerical, Date and Boolean Comparisons
Numeric range:WHERE score >= 70 AND score <= 90
Date range (ISO format yyyy‑mm‑dd):WHERE orderdate >= '2023-01-01' AND orderdate <= '2023-12-31'
Boolean field:WHERE is_active = TRUE (no quotes required for TRUE/FALSE in most exam‑level DBMS)
e‑safety: query only the data you need and keep results secure.
After a search you may sort (ORDER BY), calculate fields, and present the data in a report.
Suggested diagram: Flowchart of the query‑building process – select table → choose fields → set criteria (operators) → apply sorting / calculations → view / export results.
Support e-Consult Kenya
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources,
past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.