TopMyGrade

GCSE/Computer Science/AQA

CS7.2SQL — SELECT statements: SELECT … FROM … WHERE …; using AND/OR; ORDER BY; selecting columns and filtering rows

Notes

SQL — SELECT statements

SQL (Structured Query Language) is the standard language for querying relational databases. The most common operation is the SELECT statement, which retrieves data without changing it.

Basic structure

SELECT column1, column2
FROM tableName
WHERE condition
ORDER BY column;
  • SELECT — which columns you want.
  • FROM — which table to read from.
  • WHERE — filter rows by condition (optional).
  • ORDER BY — sort the results (optional).

Use * for "all columns": SELECT * FROM Students;.

Filtering with WHERE

Comparison operators: =, <> (or !=), <, <=, >, >=. Combine conditions with AND and OR; negate with NOT.

SELECT Name, DOB
FROM Students
WHERE TutorGroup = '10A';
SELECT * FROM Books
WHERE Author = 'Tolkien' AND Year > 1950;
SELECT *
FROM Students
WHERE TutorGroup = '10A' OR TutorGroup = '10B';

LIKE for pattern matching

SELECT * FROM Books WHERE Title LIKE 'The%';
  • % matches any sequence of characters.
  • _ matches a single character.

So 'The%' finds anything starting with "The".

IN — list membership

SELECT * FROM Students WHERE TutorGroup IN ('10A', '10B', '10C');

Equivalent to using OR repeatedly.

BETWEEN — range

SELECT * FROM Books WHERE Year BETWEEN 1990 AND 2000;

Inclusive of both endpoints.

Sorting with ORDER BY

SELECT Name, Mark FROM Students ORDER BY Mark DESC;
  • ASC — ascending (default).
  • DESC — descending.

You can sort by multiple columns:

SELECT * FROM Students ORDER BY TutorGroup ASC, Name ASC;

Selecting specific columns

SELECT Name, DOB FROM Students;

Returns just those two columns, not all data — better for performance and clarity.

Joining tables (Higher / extension)

SELECT Students.Name, Subjects.Name AS Subject, Enrolments.Grade
FROM Students
JOIN Enrolments ON Students.StudentID = Enrolments.StudentID
JOIN Subjects   ON Enrolments.SubjectID = Subjects.SubjectID
WHERE Students.TutorGroup = '10A';

A JOIN combines rows across tables using their key relationships. AQA's GCSE focuses on simple SELECTs but joins appear in some questions.

Aggregate functions (extension)

SELECT COUNT(*) FROM Students;
SELECT AVG(Mark) FROM Enrolments WHERE SubjectID = 'S01';
SELECT MIN(Year), MAX(Year) FROM Books;

COUNT, AVG, MIN, MAX, SUM — useful summaries.

Worked example

A Books table:

BookID | Title              | Author        | Year
1      | The Hobbit         | Tolkien       | 1937
2      | LOTR               | Tolkien       | 1954
3      | Foundation         | Asimov        | 1951
4      | I, Robot           | Asimov        | 1950
5      | Dune               | Herbert       | 1965

SELECT Title, Year FROM Books WHERE Author = 'Tolkien' ORDER BY Year ASC;

Returns:

Title       | Year
The Hobbit  | 1937
LOTR        | 1954

Style tips

  • Use uppercase for SQL keywords by convention.
  • Use single quotes for string literals ('10A'), not double.
  • End each statement with a semicolon.
  • Indent for readability when statements get long.

Common mistakesPitfalls

  1. Forgetting WHERE. SELECT * FROM Students; returns the entire table.
  2. Using == instead of =. SQL uses single = for equality (unlike most programming languages).
  3. Mixing AND and OR without parentheses. Operator precedence can surprise you. Use brackets to clarify.
  4. Using double quotes for strings. SQL identifies columns/tables in double quotes; strings need single.
  5. Forgetting case sensitivity. Some databases are case-sensitive on data values.

Try thisQuick check

For the Students table from CS7.1:

  • SELECT * FROM Students WHERE TutorGroup = '10A'; — returns Aisha and Chloe.
  • SELECT Name FROM Students ORDER BY DOB DESC; — returns names ordered youngest first.
  • SELECT * FROM Students WHERE Name LIKE 'A%'; — returns Aisha (starts with A).

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 13 marks

    Basic SELECT

    Write SQL to select all columns from a table called Books.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 23 marks

    SELECT with WHERE

    Write SQL to select Name and Mark from Students where the mark is greater than 70.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 33 marks

    AND / OR

    Write SQL to select all rows from Books where the author is 'Tolkien' AND the year is after 1950.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 43 marks

    ORDER BY DESC

    Write SQL to select Title and Year from Books, ordered from newest to oldest.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 53 marks

    LIKE pattern

    Write SQL to select all books whose title starts with "The".

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    BETWEEN

    Write SQL to select students whose date of birth falls between 2008-09-01 and 2009-08-31, inclusive, sorted by name.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Trace SELECT result

    Given the following Students table:
    ID | Name | TutorGroup | Mark
    1 | Aisha | 10A | 84
    2 | Ben | 10B | 67
    3 | Chloe | 10A | 92
    4 | Dan | 10C | 71

    State the result of:
    SELECT Name, Mark FROM Students WHERE TutorGroup = '10A' ORDER BY Mark DESC;

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS7.2 — SQL — SELECT statements

12-card SR deck for AQA GCSE Computer Science topic CS7.2

12 cards · spaced repetition (SM-2)