OCR H446, section 1.3.2 (d)
SQL (Structured Query Language) is the standard language for working with a relational database. In the OCR exams you must be able to interpret and modify SQL. The structures that you are expected to know are:
SELECT (including nested selects), FROM, WHERE, LIKE, AND, OR, DELETE, INSERT, DROP and JOIN (which means an inner join), with the wildcards * and %. If anything else is used, it will be explained in the question.
The examples use this small library database.
BOOK
| BookID | Title | AuthorID | Year |
|---|---|---|---|
| 1 | Dune | 10 | 1965 |
| 2 | Emma | 11 | 1815 |
| 3 | Persuasion | 11 | 1817 |
| 4 | Neuromancer | 12 | 1984 |
AUTHOR
| AuthorID | Name | Country |
|---|---|---|
| 10 | Frank Herbert | USA |
| 11 | Jane Austen | UK |
| 12 | William Gibson | USA |
AuthorID in BOOK is a foreign key that links to AUTHOR.
SELECT, FROM and WHERE
SELECT columns
FROM table
WHERE condition;
SELECT Title, Year
FROM Book
WHERE Year > 1900;
| Title | Year |
|---|---|
| Dune | 1965 |
| Neuromancer | 1984 |
SELECT * gives all columns. * is a wildcard meaning "everything".
AND and OR
SELECT Title FROM Book
WHERE Year > 1800 AND Year < 1900;
Gives Emma and Persuasion. OR gives rows that match either condition. Use brackets to make the order clear when mixing them.
LIKE and wildcards
LIKE matches a pattern. The wildcard % stands for any number of characters.
SELECT Title FROM Book
WHERE Title LIKE 'P%';
Gives Persuasion (titles that start with P). '%a' matches titles ending in a, and '%an%' matches titles containing "an". (Some systems use * instead of %.)
JOIN: using more than one table
Data is split across tables, so a query often needs to join them, matching the foreign key to the primary key.
SELECT Book.Title, Author.Name
FROM Book
INNER JOIN Author ON Book.AuthorID = Author.AuthorID;
| Title | Name |
|---|---|
| Dune | Frank Herbert |
| Emma | Jane Austen |
| Persuasion | Jane Austen |
| Neuromancer | William Gibson |
Book.Title means the Title column of the Book table. The ON condition says how the tables are linked. JOIN on its own means the same as INNER JOIN.
Joined with a condition:
SELECT Book.Title
FROM Book
JOIN Author ON Book.AuthorID = Author.AuthorID
WHERE Author.Country = 'UK';
Gives Emma and Persuasion.
Nested SELECT (a subquery)
A SELECT can be placed inside another. The inner query is worked out first, and its result is used by the outer one.
SELECT Title
FROM Book
WHERE AuthorID IN (SELECT AuthorID
FROM Author
WHERE Country = 'USA');
- The inner query finds the AuthorIDs of American authors: 10 and 12.
- The outer query finds the books whose
AuthorIDis in that list: Dune and Neuromancer.
The same result can be found with a JOIN.
INSERT
Adds a new record.
INSERT INTO Book (BookID, Title, AuthorID, Year)
VALUES (5, 'Mansfield Park', 11, 1814);
The values are in the same order as the column names, and text is in quotes. The AuthorID (11) must already exist in Author.
DELETE
Removes records.
DELETE FROM Book
WHERE BookID = 4;
WHERE chooses which records to delete. Without it, DELETE FROM Book; removes every record.
DROP
Removes a whole table (its structure and all its data).
DROP TABLE Book;
This is much more drastic than DELETE, which keeps the table and only removes rows.
Changing existing data: UPDATE
UPDATE isn't on OCR's list, but it's the usual way to change existing data, and it has been asked without any explanation (students were asked to write a statement to change a value in a table). So learn it.
UPDATE Book
SET Year = 1816
WHERE Title = 'Persuasion';
SET gives the new value, and WHERE says which rows to change. Without WHERE, every row is changed.
Interpreting a query
To work out what a query does, go clause by clause:
FROM(andJOIN): which table(s)?WHERE: which rows are kept?SELECT: which columns are shown?
Example: what does this give?
SELECT Title
FROM Book
WHERE Year < 1900 AND Title LIKE '%n%';
- Books before 1900: Emma (1815) and Persuasion (1817).
- Titles containing the letter n: Persuasion has an n. Emma doesn't.
- The result is Persuasion.
Worked example
Task: write SQL to (a) list the names of authors from the UK, (b) show the title and author name of every book published before 1900, (c) delete the author with AuthorID 12.
(a)
SELECT Name FROM Author
WHERE Country = 'UK';
(b)
SELECT Book.Title, Author.Name
FROM Book
JOIN Author ON Book.AuthorID = Author.AuthorID
WHERE Book.Year < 1900;
(c)
DELETE FROM Author
WHERE AuthorID = 12;
Notice that (c) would break referential integrity if Neuromancer (AuthorID 12) is still in Book, because the book would refer to an author who no longer exists (see 1.3.2 (e)). The book should be deleted first, or the system would refuse.
Exam tip: write the clauses in order (SELECT, FROM, JOIN ... ON, WHERE). In a join, state the link between the foreign key and the primary key. Text values need quotes. In DELETE and UPDATE, don't forget the WHERE.
Check yourself
Write SQL to show all columns for books published in 1815
SELECT * FROM Book WHERE Year = 1815;
What does LIKE 'S%' match?
Any text that starts with S, followed by any number of other characters.
What is the difference between DELETE and DROP?
DELETE removes records from a table, and the table stays. DROP removes the whole table, including its structure and data.
Why does a query with a JOIN include an ON condition?
It says how the tables are linked, by matching the foreign key in one table with the primary key in the other, so the right rows are combined.