Revision Notes
AQA A-Level Computer Science (7517): Structured Query Language (SQL) – Revision Notes
Condensed AQA 7517 section 4.10.4 recall: SELECT, joins, aggregates, GROUP BY, HAVING, INSERT, UPDATE, DELETE and CREATE TABLE, with common errors.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of databases
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.10.4 Structured Query Language (SQL)
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.10.4 Structured Query Language (SQL), AQA A-Level Computer Science (7517): use SQL to retrieve, update, insert and delete data from multiple tables and to define a table. Worked queries with checked results are in the SQL study guide; questions are in the SQL practice questions.
SELECT: clause order
SELECT columns or expressions
FROM tables (with joins)
WHERE condition on rows
GROUP BY columns
HAVING condition on groups
ORDER BY columns [ASC | DESC]
- Only SELECT and FROM are compulsory; keep the clauses in this order.
- Text and dates go in single quotes; numbers do not.
*means all columns.
Conditions
| Need | Syntax |
|---|---|
| Comparison | =, <>, <, >, <=, >= |
| Combine | AND, OR, NOT (AND is evaluated before OR; use brackets) |
| Pattern | LIKE 'B%' (% = any number of characters) |
| Range or list | BETWEEN 10 AND 20, IN ('Leeds','York') |
| Empty value | IS NULL, IS NOT NULL (never = NULL) |
| Sub-query | WHERE ID NOT IN (SELECT ID FROM Other) |
Aggregate functions
COUNT(*), SUM(col), AVG(col), MAX(col), MIN(col)
- GROUP BY gives one result row per distinct value of the group column.
- WHERE filters rows before grouping. HAVING filters groups after grouping, so aggregate conditions go in HAVING.
- Any column in SELECT that is not inside an aggregate must be in GROUP BY.
Joins across tables
SELECT Member.MemberName, Book.Title
FROM Member
INNER JOIN Loan ON Member.MemberID = Loan.MemberID
INNER JOIN Book ON Loan.ISBN = Book.ISBN
WHERE Loan.DateBack IS NULL
- One
INNER JOIN ... ONper link; match a foreign key to the primary key it refers to. - Qualify a column with its table name if the name is not unique.
- Follow the chain of foreign keys to decide which tables are needed.
- Without an ON condition every row pairs with every row.
Changing data
INSERT INTO Member (MemberID, MemberName, Year)
VALUES (5, 'Eli Park', 11)
UPDATE Book
SET CopiesOwned = CopiesOwned + 1
WHERE Genre = 'Science fiction'
DELETE FROM Loan
WHERE DateBack IS NOT NULL
- Always include WHERE in UPDATE and DELETE, or every row is changed or removed.
- INSERT: columns listed first, values in the same order.
- Key rules (primary key, foreign key) can reject changes that break them.
Defining a table
CREATE TABLE Loan (
LoanID INTEGER NOT NULL,
MemberID INTEGER NOT NULL,
ISBN VARCHAR(5) NOT NULL,
DateOut DATE NOT NULL,
DateBack DATE,
PRIMARY KEY (LoanID),
FOREIGN KEY (MemberID) REFERENCES Member(MemberID),
FOREIGN KEY (ISBN) REFERENCES Book(ISBN)
)
| Data type | Use |
|---|---|
INTEGER |
Whole numbers |
VARCHAR(n) |
Text up to n characters |
CHAR(n) |
Text of fixed length |
DATE |
Dates |
DECIMAL(p,s) or REAL |
Numbers with decimals |
BOOLEAN |
True or false |
- A composite key:
PRIMARY KEY (OrderID, ProductCode). - Create the referenced table first.
Checked results (library data)
For the practice database (4 members, 4 books, 6 loans, three not returned):
- Loans not yet returned:
SELECT COUNT(*) FROM Loan WHERE DateBack IS NULLgives 3. - Total copies owned: 8; average 2.0; maximum 3.
Building a query step by step
- Decide which columns the answer must show and write the SELECT line.
- Decide which tables hold those columns and any columns used in conditions.
- If more than one table is needed, add a join for each foreign key link.
- Write the WHERE condition for rows, using quotes for text.
- If the question asks “how many” or “total” per something, add the aggregate and GROUP BY.
- If the condition is on the aggregate, add HAVING.
- Add ORDER BY last, with DESC if largest first.
- Test the query by hand on two or three rows of the sample data.
Key terms
| Term | Definition |
|---|---|
| Query | A request to a database to retrieve or change data |
| Join | Combining rows of two tables by matching a key |
| Aggregate function | A function that summarises many rows into one value |
| Alias | A temporary name given with AS |
| NULL | The absence of a value |
| Sub-query | A query inside another query |
| Clause | One part of an SQL statement, such as WHERE |
| Reserved word | A word with special meaning in SQL that cannot be used as a table name |
| Data type | The kind of value a column may hold |
Exam traps
- Aggregate in WHERE. Use HAVING.
- Missing ON in a join.
- No WHERE on UPDATE or DELETE.
- Double quotes or no quotes around text.
= NULLinstead ofIS NULL.- Ungrouped column beside an aggregate.
- Wrong clause order (HAVING before GROUP BY, for example).
- Joining non-key columns that do not match primary and foreign keys.
Self-test
- What does the WHERE clause do?
- Which clause sorts the results?
- When is HAVING used instead of WHERE?
- Write the condition for names starting with B.
- How is a record added to a table?
- What happens if an UPDATE has no WHERE?
- Why must a join have an ON condition?
- How do you test for an empty value?
- What does
SUM(Quantity)return? - In CREATE TABLE, how is a composite primary key written?
- Which table is created first: the one with the foreign key, or the one it refers to?
- What does
DESCdo?
Answers
- It selects only the rows that satisfy a condition.
- ORDER BY.
- When the condition uses an aggregate or applies to groups after GROUP BY.
LIKE 'B%'.- With
INSERT INTO table (columns) VALUES (values). - Every row is updated.
- Otherwise every row of one table is paired with every row of the other.
IS NULL.- The total of the Quantity values in the rows selected (or in each group).
PRIMARY KEY (col1, col2).- The table it refers to.
- Sorts the results from largest to smallest.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Client Server Databases
Deeper guide to AQA 7517 section 4.10.5: client server database systems, the lost update problem and controlling concurrent access with record locks, serialisation, timestamp ordering and commitment ordering.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Client Server Databases – Practice Questions
Original AQA 7517 section 4.10.5 questions with answers: client server databases, lost updates and record locks, serialisation, timestamps and commitment ordering.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Client Server Databases – Revision Notes
Condensed AQA 7517 section 4.10.5 recall: client server database systems, lost updates and the control methods of record locks, serialisation, timestamp ordering and commitment ordering.
Computer Science · AQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.