Algorithms and programming · GCSE Computer Science

SQL SELECT queries

GCSE Computer Science SQL: SELECT, FROM and WHERE, the difference between = and LIKE, and how to write a query that actually matches the table in the paper.

UNDERSTANDRETRIEVEREMEMBER
THE MEMORY HOOK
SELECT the fields, FROM the table, WHERE the condition. = is an exact match. LIKE uses wildcards for a pattern. Miss a quote around text and the query dies.

The important bits

What you need to know

  1. 1

    SQL (Structured Query Language) asks a relational database for rows. The core GCSE pattern is SELECT fields FROM table WHERE condition;

  2. 2

    SELECT names the columns you want, or SELECT * for every column. Only list the fields the question asked for — extra columns can lose a mark on a strict scheme.

  3. 3

    FROM names the table. Spell it as printed, including capitals if the paper is that fussy. Joining two tables (if required) uses a matching field, often a primary/foreign key.

  4. 4

    WHERE filters rows. Text and dates sit in quotes: WHERE city = 'Leeds'. Numbers do not: WHERE age >= 16. Mixing those quote rules is a free error.

  5. 5

    = tests exact equality. LIKE tests a pattern: 'Sm%' matches Smith and Smythe; '_at' matches cat and hat. % is any number of characters; _ is one character (board examples vary slightly).

  6. 6

    AND means both conditions must be true; OR means at least one. Brackets help: WHERE year = 11 AND (grade = 'A' OR grade = 'B').

  7. 7

    ORDER BY field ASC or DESC sorts the result. It does not change the stored table. DISTINCT (if taught) removes duplicate rows in the output.

  8. 8

    SQL injection is a security cousin: if user input is concatenated into the WHERE clause, an attacker can change the query. Parameterised queries belong in the prevention list.

Quotations worth analysing

Short evidence. Real method.

SELECT fields FROM table WHERE condition;
GCSE SQL skeleton

Three clauses, in that order. WHERE is optional if you want every row. A missing FROM is not a SELECT query.

= matches an exact value; LIKE matches a pattern.
Mark-scheme comparison

WHERE name = 'Smith' misses Smythe. WHERE name LIKE 'Sm%' catches both if that is the intent. Use the one the question’s English requires.

Text values are enclosed in quotation marks.
SQL syntax mark

WHERE city = Leeds without quotes looks like a field name. WHERE age = '16' may fail as a type mismatch. Quotes around strings, not around numbers.

Go deeper

Read the table, then write the smallest honest SELECT

The paper prints a table Student with fields ID, Forename, Surname, Year, Grade. “List the surnames of students in year 11” is SELECT Surname FROM Student WHERE Year = 11; — not SELECT * unless they asked for all fields. “Students whose surname begins with B” is LIKE 'B%', not = 'B'. Case can matter depending on the system; copy the case of the sample data. If two conditions must hold, AND; if either is enough, OR. Students stack AND when the English said “year 11 or year 12”. Translate the sentence into Boolean logic before you type SQL. ORDER BY Surname is only needed if they asked for alphabetical output. Do not add JOIN, GROUP BY or inner mysteries the course did not teach; a correct three-clause query beats an impressive broken one.

Go deeper

LIKE wildcards and the equals trap

LIKE 'A%' means starts with A. LIKE '%son' means ends with son. LIKE '%mid%' means contains mid. The underscore is a single character if your specification uses it: LIKE 'J_n' matches Jan and Jon, not Johan. Using = with a percent sign looks for a literal percent in the data, which is almost never what you wanted. Using LIKE without a wildcard is just a fussy equals. When the question says “exactly 16”, that is = 16, not LIKE. When it says “the word cat anywhere in the description”, that is LIKE '%cat%'. Write the quotes. End with a semicolon if the paper’s examples do. If they show field names in Capitals, copy them. Markers compare your query to a short list of acceptable spellings; creative aliases do not score unless asked.

WORKED EXAMPLE

See the idea in action

Table Product: ProductID, Name, Category, Price. Rows: (1, 'Biro', 'Stationery', 0.40), (2, 'Binder', 'Stationery', 2.50), (3, 'Ball', 'Sport', 5.00). Task: names and prices of stationery items costing under £2. SELECT Name, Price FROM Product WHERE Category = 'Stationery' AND Price < 2; Row 1: Category matches, 0.40 < 2, include Biro, 0.40. Row 2: Category matches, 2.50 < 2 is false, exclude Binder. Row 3: Category is Sport, exclude even though 5.00 is a number you glanced at. Result: Biro, 0.40. If you used LIKE 'S%' instead of = 'Stationery', Sport would sneak in. If you used = 'stationery' and the database is case-sensitive, you might get nothing. If you wrote WHERE Name LIKE 'B%' you would pull Biro, Binder and Ball — a different question.

Exam technique

Turn knowledge into marks

SELECT only the fields asked for, FROM the printed table name, WHERE with quoted text and unquoted numbers. Use LIKE with % when the question says “starts with” or “contains”; use = for an exact match.

Common mistakes

Do not give these marks away

  1. 01

    Using = when the question needed a pattern match with LIKE, or putting % inside an = comparison.

  2. 02

    Forgetting quotes around text, or putting quotes around a numeric field.

  3. 03

    SELECT * when the question named specific fields, or filtering with OR when the English required both conditions (AND).

QUICK RETRIEVAL

Which SQL clause finds surnames that begin with Sm?

AWHERE Surname = 'Sm%'

BWHERE Surname LIKE 'Sm%'

CWHERE Surname = Sm

DSELECT Sm FROM Surname

Show the answer

WHERE Surname LIKE 'Sm%'. LIKE with % is a pattern. = 'Sm%' looks for those exact characters including a percent sign. = Sm without quotes is invalid. SELECT Sm FROM Surname has the field and table the wrong way round.

Quick questions

If this is the bit you searched

What is the difference between = and LIKE in SQL GCSE?

= tests exact equality. LIKE matches a pattern using wildcards such as % (any number of characters). Use LIKE for “starts with” or “contains”; use = for an exact value.

How do you write a basic SELECT query?

SELECT field list FROM table WHERE condition; Omit WHERE if you want every row. Put quotation marks around text values, not around numbers.

What does SELECT * mean?

Return every column from the table. If the question asked for named fields only, list those fields instead of *.

Do I need a semicolon at the end of SQL?

Follow the paper’s examples. Many GCSE mark schemes accept the query with or without a trailing semicolon if the clauses are correct.