AQA 7517, section 4.10.4
SQL (Structured Query Language) is the standard language for working with a relational database. You use it to retrieve data, insert new data, update existing data, delete data, and define tables.
The examples use this small school database.
STUDENT
| StudentID | Name | YearGroup | TutorID |
|---|---|---|---|
| 1 | Asha Khan | 12 | 10 |
| 2 | Ben Ortiz | 12 | 11 |
| 3 | Cara Lee | 13 | 10 |
| 4 | Dev Patel | 13 | 12 |
TUTOR
| TutorID | TutorName | Room |
|---|---|---|
| 10 | Ms Green | A1 |
| 11 | Mr Brown | B2 |
| 12 | Mrs White | C3 |
TutorID in STUDENT is a foreign key to TUTOR.
Retrieving data: SELECT
The basic form is
SELECT <columns>
FROM <table>
WHERE <condition>
ORDER BY <column>;
The words in angle brackets are placeholders for your own column names, table name and condition. WHERE and ORDER BY are optional.
SELECT Name, YearGroup
FROM Student;
Gives every student's name and year group. Use * to select all columns.
WHERE: choosing rows
SELECT Name
FROM Student
WHERE YearGroup = 13;
| Name |
|---|
| Cara Lee |
| Dev Patel |
Conditions can use =, <> (not equal), <, >, <=, >=, AND, OR, NOT, BETWEEN, and LIKE for patterns (% matches any characters).
SELECT * FROM Student
WHERE YearGroup = 12 AND Name LIKE 'A%';
Gives students in year 12 whose name begins with A (here, Asha Khan).
Text, dates and times go in quotes; numbers don't. For example, to find the loans made on one day from a Loan table:
SELECT StudentID, ISBN
FROM Loan
WHERE DateBorrowed = '02/11/2026';
(The exact date format depends on the database system; in an exam, write the date in quotes as it appears in the question.)
ORDER BY: sorting
SELECT Name FROM Student
ORDER BY Name DESC;
ASC is ascending (the default), and DESC is descending.
Using more than one table: JOIN
Data is split over tables, so a query often needs to join them. Match the foreign key to the primary key it refers to.
SELECT Student.Name, Tutor.TutorName
FROM Student
INNER JOIN Tutor ON Student.TutorID = Tutor.TutorID;
The result:
| Name | TutorName |
|---|---|
| Asha Khan | Ms Green |
| Ben Ortiz | Mr Brown |
| Cara Lee | Ms Green |
| Dev Patel | Mrs White |
Student.Name means "the Name column of the Student table". The ON condition says how the tables are linked.
You can combine a join with WHERE:
SELECT Student.Name
FROM Student
INNER JOIN Tutor ON Student.TutorID = Tutor.TutorID
WHERE Tutor.TutorName = 'Ms Green';
Gives Asha Khan and Cara Lee.
An older style lists the tables and puts the link in the WHERE:
SELECT Student.Name, Tutor.TutorName
FROM Student, Tutor
WHERE Student.TutorID = Tutor.TutorID;
Joining three tables (through a link table)
With a many-to-many relationship, such as students borrowing books through a Loan table, join all three:
SELECT Student.Name, Book.Title
FROM Student
INNER JOIN Loan ON Student.StudentID = Loan.StudentID
INNER JOIN Book ON Loan.ISBN = Book.ISBN;
Aggregates and grouping
SELECT COUNT(*) FROM Student WHERE YearGroup = 12;
Gives 2. Other functions are SUM, AVG, MAX and MIN. Use GROUP BY to apply them to each group:
SELECT YearGroup, COUNT(*)
FROM Student
GROUP BY YearGroup;
Inserting data: INSERT
INSERT INTO Student (StudentID, Name, YearGroup, TutorID)
VALUES (5, 'Eve Adams', 12, 11);
The values are given in the same order as the column list, and text is in quotes. The new row is added to the table. The TutorID of 11 must exist in Tutor (referential integrity).
Updating data: UPDATE
UPDATE Student
SET YearGroup = 13
WHERE StudentID = 2;
SET gives the new value, and WHERE says which rows to change. Without a WHERE, every row would be changed.
Deleting data: DELETE
DELETE FROM Student
WHERE StudentID = 4;
Again, WHERE selects which rows to remove. DELETE FROM Student; with no condition removes every row.
Watch out: UPDATE and DELETE without a WHERE clause affect all the rows in the table. Always check the condition.
Defining a table: CREATE TABLE
CREATE TABLE Tutor (
TutorID INTEGER PRIMARY KEY,
TutorName VARCHAR(30) NOT NULL,
Room VARCHAR(5)
);
CREATE TABLE Student (
StudentID INTEGER PRIMARY KEY,
Name VARCHAR(40) NOT NULL,
YearGroup INTEGER,
TutorID INTEGER,
FOREIGN KEY (TutorID) REFERENCES Tutor(TutorID)
);
Each column has a name and a data type:
| Data type | Holds |
|---|---|
INTEGER |
Whole numbers |
REAL / DECIMAL |
Numbers with a fractional part |
VARCHAR(n) / CHAR(n) |
Text of up to (or exactly) characters |
DATE |
A date |
BOOLEAN |
True or false |
Constraints include PRIMARY KEY, FOREIGN KEY ... REFERENCES, and NOT NULL (the value can't be empty). A composite primary key is written as PRIMARY KEY (StudentID, ISBN). Tutor is created first, because Student refers to it.
(Details such as the exact data type names can vary a little between database systems.)
Worked example
Task: write SQL to (a) list the names of tutors with the room each is in, ordered by room, (b) add a tutor, (c) show the names of year 13 students with their tutor's name.
(a)
SELECT TutorName, Room
FROM Tutor
ORDER BY Room;
(b)
INSERT INTO Tutor (TutorID, TutorName, Room)
VALUES (13, 'Dr Black', 'D4');
(c)
SELECT Student.Name, Tutor.TutorName
FROM Student
INNER JOIN Tutor ON Student.TutorID = Tutor.TutorID
WHERE Student.YearGroup = 13;
Result for (c): Cara Lee with Ms Green, and Dev Patel with Mrs White.
Exam tip: write the clauses in order (SELECT, FROM, JOIN, WHERE, ORDER BY). For a join, state the link between the foreign key and primary key. In UPDATE and DELETE, don't forget the WHERE. Text values need quotes. Each clause earns marks (the columns, the tables, the join condition, each criterion, ORDER BY), so write every part even if you're unsure of one. Dates go in quotes too.
Check yourself
Write SQL to show all columns for students in year 12
SELECT * FROM Student WHERE YearGroup = 12;
Why does a query usually join tables on a foreign key and a primary key?
The data is split across tables and linked by keys, so matching the foreign key to the primary key it refers to puts the related rows back together.
What would DELETE FROM Student; do?
It would remove every row from the Student table, because there is no WHERE clause to select which rows to delete.
Write SQL to change Ben Ortiz's tutor to the tutor with ID 12
UPDATE Student SET TutorID = 12 WHERE Name = 'Ben Ortiz'; (or WHERE StudentID = 2).