CIE AS & A Level Computer Science, section 8.3 (Data Definition Language (DDL) and Data Manipulation Language (DML))
The data used in these examples
Member(MemberID, Name, Year): (1, Aisha, 12), (2, Ben, 13), (3, Chloe, 12). Book(BookID, Title, Price): (10, Dune, 7.5), (11, Emma, 5.0), (12, Ulysses, 9.0). Loan(LoanID, MemberID, BookID, DateOut): (100, 1, 10, '2026-09-01'), (101, 1, 11, '2026-09-03'), (102, 2, 10, '2026-09-15'), (103, 3, 12, '2026-09-20').
SELECT ... FROM ... WHERE
SELECT Name FROM Member WHERE Year = 12 ORDER BY Name;
Result: Aisha, Chloe. ORDER BY sorts the result (add DESC for descending).
SELECT Title, Price FROM Book WHERE Price > 6 ORDER BY Price DESC;
Result: (Ulysses, 9.0), (Dune, 7.5).
Functions
SELECT SUM(Price), AVG(Price), COUNT(*) FROM Book;
Result: , , .
GROUP BY
SELECT Year, COUNT(*) FROM Member GROUP BY Year;
Result: (12, 2), (13, 1). GROUP BY groups rows with the same value, so the function is calculated for each group.
INNER JOIN
A join combines rows from two tables where a condition matches.
SELECT Member.Name, Book.Title
FROM Member
INNER JOIN Loan ON Member.MemberID = Loan.MemberID
INNER JOIN Book ON Loan.BookID = Book.BookID
ORDER BY Member.Name;
Result: (Aisha, Dune), (Aisha, Emma), (Ben, Dune), (Chloe, Ulysses).
Queries can use at most two tables in the exam, so one INNER JOIN is the most you will need to write.
Common mistake: forgetting the join condition. INNER JOIN needs ON with the matching key fields; without it you get every combination of rows. Also, use WHERE for tests on single rows, and GROUP BY before using a function on groups.
Exam tip: write the clauses in the order SELECT, FROM, WHERE, GROUP BY, ORDER BY. Prefix field names with the table name (Member.Name) when both tables have the same field.
Check yourself
Write a query for the titles of books costing less than 8.
SELECT Title FROM Book WHERE Price < 8;
What does GROUP BY do?
It groups rows with the same value in a field so that a function such as COUNT can be applied to each group.
What does COUNT(*) return?
The number of rows in the result.