How do we query and modify a relational database using SQL?
Understand SQL for retrieving data with SELECT, FROM, WHERE and ORDER BY, joining tables, and modifying data with INSERT, UPDATE and DELETE.
A focused answer to AQA A-Level Computer Science 4.10.3 and 4.10.4, covering SQL for retrieving data with SELECT, FROM, WHERE and ORDER BY, joining tables, and modifying data with INSERT, UPDATE and DELETE.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
What this dot point is asking
AQA wants you to write SQL to retrieve data (SELECT, FROM, WHERE, ORDER BY), join tables, and modify data with INSERT, UPDATE and DELETE.
Retrieving data
SELECT Name, Mark
FROM Student
WHERE Mark >= 50
ORDER BY Mark DESC;
The WHERE clause can combine conditions with AND, OR and NOT, and use LIKE for pattern matching (with wildcards) and BETWEEN for ranges. Because SQL is declarative, you describe the result set you want and the database management system decides how to find it efficiently, which is the practical example of the declarative paradigm covered in the languages topic.
Joining tables
SELECT Student.Name, Course.Title
FROM Student, Course
WHERE Student.CourseID = Course.CourseID;
The join is the direct payoff of normalisation: because data was split into separate tables to remove redundancy, a join is needed to recombine it for a report. The join condition follows the same foreign-key-to-primary-key link that the relationship was built on, which is why you must know the keys to write a correct join. Forgetting the condition produces a Cartesian product, every combination of rows, which is almost never what is wanted.
Modifying data
The single most dangerous mistake in this topic is omitting the WHERE clause from an UPDATE or DELETE, because the command then silently affects every row in the table. This connects to transaction processing: such bulk changes are exactly why databases wrap modifications in transactions that can be rolled back, and why ACID properties matter when many users change data at once.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20195 marksA database has a Student table (StudentID, Name, CourseID) and a Course table (CourseID, Title). Write an SQL query to list the name of each student and the title of their course, for students on the course with the title 'Computing', sorted by student name. Use a join.Show worked answer →
A correct query joins the two tables on the matching CourseID, filters by course title, and sorts by name:
SELECT Student.Name, Course.Title FROM Student, Course WHERE Student.CourseID = Course.CourseID AND Course.Title = 'Computing' ORDER BY Student.Name;
The join condition Student.CourseID = Course.CourseID matches each student to their course; the extra condition Course.Title = 'Computing' filters to the required course; ORDER BY Student.Name sorts the output.
Markers reward selecting the two correct fields, the join condition matching the foreign key to the primary key, the filter on the title, and the sort. Omitting the join condition (which would produce a Cartesian product) loses the join mark.
AQA 20214 marksWrite SQL statements to (a) add a new student called 'Lee' with StudentID 105 and CourseID 3 to the Student table, and (b) delete all students whose CourseID is 9. Explain the risk of omitting the WHERE clause in part (b).Show worked answer →
(a) INSERT INTO Student (StudentID, Name, CourseID) VALUES (105, 'Lee', 3);
(b) DELETE FROM Student WHERE CourseID = 9;
If the WHERE clause were omitted from the DELETE statement, the command would become DELETE FROM Student;, which removes every record in the Student table rather than only those with CourseID 9, causing total data loss. The WHERE clause is essential to restrict the operation to the intended rows.
Markers reward the correct INSERT with matching columns and values, the correct DELETE with the WHERE condition, and the explanation that omitting WHERE deletes the entire table.
Related dot points
- Understand relational databases, primary and foreign keys, the problems of data redundancy, and normalisation to first, second and third normal form.
A focused answer to AQA A-Level Computer Science 4.10.2, covering relational databases, primary and foreign keys, the problems of data redundancy, and normalisation to first, second and third normal form.
- Understand conceptual data modelling, entities, attributes and relationships, entity relationship (ER) diagrams, and the degrees of relationship (one-to-one, one-to-many, many-to-many).
A focused answer to AQA A-Level Computer Science 4.10.1, covering conceptual data modelling, entities, attributes and relationships, entity relationship diagrams, and the degrees of relationship including resolving many-to-many.
- Understand transaction processing, the ACID properties, record locking and the deadlock that can result, redundancy and database recovery.
A focused answer to AQA A-Level Computer Science 4.10.5, covering transaction processing, the ACID properties, record locking and deadlock, redundancy, and database recovery from failure.
- Understand the client-server and peer-to-peer models, web technologies including HTML, CSS and JavaScript, the role of web servers, and the use of APIs and thin versus thick clients.
A focused answer to AQA A-Level Computer Science 4.8.6, covering the client-server and peer-to-peer models, web technologies including HTML, CSS and JavaScript, the role of web servers, and thin versus thick clients.
- Understand the classification of programming languages by level (low and high) and by paradigm (imperative, object-oriented, declarative and functional), and the use of machine code and assembly language.
A focused answer to AQA A-Level Computer Science 4.6.4, covering the classification of programming languages by level (low and high) and by paradigm (imperative, object-oriented, declarative and functional), and the use of machine code and assembly language.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)