How do we retrieve and change data in a relational database using SQL?
Write SQL queries using SELECT with WHERE, ORDER BY, JOIN, GROUP BY and aggregate functions, and modify data with INSERT, UPDATE and DELETE
A focused answer to the H2 Computing outcome on SQL queries. SELECT with WHERE and ORDER BY, joining tables, grouping with aggregate functions, and modifying rows 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
Jump to a section
What this dot point is asking
SEAB wants you to write SQL that retrieves data with SELECT, filters with WHERE, sorts with ORDER BY, combines tables with JOIN, summarises with GROUP BY and aggregate functions, and changes data with INSERT, UPDATE and DELETE. The core idea is that SQL is declarative: you describe the result you want, and the database works out how to produce it.
The answer
SELECT, WHERE and ORDER BY
The SELECT statement reads data. Choose columns, the source table, an optional filter and an optional sort:
SELECT Name, ClassID
FROM Student
WHERE ClassID = 3
ORDER BY Name ASC;
WHERE keeps only rows matching a condition (=, <, >, LIKE, AND, OR). ORDER BY sorts the result, ascending by default or DESC for descending.
JOIN across tables
A JOIN combines rows from two tables where a foreign key matches a primary key:
SELECT Student.Name, Class.ClassName
FROM Student
JOIN Class ON Student.ClassID = Class.ClassID;
The ON clause states the matching condition. Always qualify column names with the table when they could be ambiguous.
GROUP BY and aggregate functions
Aggregate functions summarise many rows into one value: COUNT, SUM, AVG, MIN, MAX. GROUP BY applies them per group:
SELECT ClassID, COUNT(*) AS NumStudents, AVG(Mark) AS MeanMark
FROM Student
GROUP BY ClassID;
Use HAVING to filter groups after aggregation (whereas WHERE filters rows before).
INSERT, UPDATE and DELETE
These change the data:
INSERT INTO Student (StudentID, Name, ClassID)
VALUES (5012, 'Aisyah Rahman', 3);
UPDATE Student SET ClassID = 4 WHERE ClassID = 3;
DELETE FROM Student WHERE StudentID = 5012;
The WHERE clause on UPDATE and DELETE decides which rows are affected. Omit it and the statement changes or removes every row.
Examples in context
Example 1. A reporting dashboard. A sales dashboard runs a single grouped query - join orders to products, group by category, sum the revenue - to turn thousands of raw order rows into a short summary table. The same data underlies many reports just by changing the grouping column.
Example 2. Safe bulk edits. When a school renames a class, an administrator runs one UPDATE ... WHERE ClassID = ... instead of editing rows by hand. The WHERE clause is what makes the change precise; testing the matching SELECT first confirms exactly which rows will be touched.
Try this
Q1. Write SQL to list the names of students with a mark above 80, sorted highest first, from Student(Name, Mark). [2 marks]
- Cue.
SELECT Name FROM Student WHERE Mark > 80 ORDER BY Mark DESC;
Q2. What does an UPDATE statement do if you forget the WHERE clause? [1 mark]
- Cue. It updates every row in the table, not just the intended ones.
Q3. Which clause filters groups produced by GROUP BY? [1 mark]
- Cue.
HAVINGfilters aggregated groups, after grouping;WHEREfilters rows before.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original6 marksA database has tables Student(StudentID, Name, ClassID) and Class(ClassID, ClassName). Write SQL to (a) list the names of all students in the class named '12A', and (b) list each class name with the number of students in it, ordered from largest class to smallest.Show worked answer →
(a) Join the tables on the shared key and filter by class name:
SELECT Student.Name
FROM Student
JOIN Class ON Student.ClassID = Class.ClassID
WHERE Class.ClassName = '12A';
(b) Group the joined rows by class and count, then order by the count descending:
SELECT Class.ClassName, COUNT(*) AS NumStudents
FROM Class
JOIN Student ON Student.ClassID = Class.ClassID
GROUP BY Class.ClassName
ORDER BY NumStudents DESC;
Markers reward the correct join condition on ClassID, the WHERE filter on the class name, and in (b) the GROUP BY with COUNT(*) and a descending ORDER BY.
Original4 marksUsing the same Student table, write SQL to (a) insert a new student with StudentID 5012, name 'Aisyah Rahman' and ClassID 3, and (b) move every student currently in ClassID 3 to ClassID 4. State why a WHERE clause matters in the update.Show worked answer →
(a) Insert a single row, listing the columns and matching values:
INSERT INTO Student (StudentID, Name, ClassID)
VALUES (5012, 'Aisyah Rahman', 3);
(b) Update the rows that match the condition:
UPDATE Student
SET ClassID = 4
WHERE ClassID = 3;
The WHERE clause is essential: without it, UPDATE changes every row in the table, so all students would be moved to ClassID 4, not just those who were in ClassID 3. The same warning applies to DELETE.
Markers reward the column list and matching values in the insert, the conditional update, and the explanation that an omitted WHERE affects every row.
Related dot points
- Describe the relational model in terms of tables, rows, attributes, primary keys and foreign keys, and explain referential integrity
A focused answer to the H2 Computing outcome on the relational model. Tables, rows and attributes, primary and foreign keys, relationships between tables, and how referential integrity keeps data consistent.
- Define database schemas with CREATE TABLE, choosing data types and enforcing PRIMARY KEY, FOREIGN KEY, NOT NULL and UNIQUE constraints
A focused answer to the H2 Computing outcome on defining schemas. CREATE TABLE, choosing appropriate data types, and enforcing primary key, foreign key, NOT NULL and UNIQUE constraints to protect data integrity.
- Normalise a relational design to first, second and third normal form, explaining the anomalies each form removes
A focused answer to the H2 Computing outcome on normalisation. First, second and third normal form, the insertion, update and deletion anomalies they remove, and how to decompose a table step by step.
- Model a problem domain with an entity-relationship diagram, identifying entities, attributes, relationships and cardinality, and map it to tables
A focused answer to the H2 Computing outcome on ER modelling. Entities, attributes and relationships, one-to-one, one-to-many and many-to-many cardinality, resolving many-to-many with a link table, and mapping an ER model to relational tables.
- Define classes with attributes and methods, create objects, and apply encapsulation, inheritance and polymorphism in Python
A focused answer to the H2 Computing outcome on object-oriented programming. Classes and objects, attributes and methods, the constructor, and the principles of encapsulation, inheritance and polymorphism in Python.