Write an SQL script that queries or modifies data (DML) stored in no more than two database tables.
| Algebra | SQL equivalent |
|---|---|
| σcondition(R) (selection) | SELECT … FROM R WHERE condition |
| πattributes(R) (projection) | SELECT attribute1, attribute2 … FROM R |
| R ⋈condition S (join) | FROM R JOIN S ON condition |
| γgroup, agg(R) (aggregation) | SELECT … FROM R GROUP BY … HAVING … |
| Command | Purpose | Example |
|---|---|---|
| CREATE TABLE | Define a new table, its columns and constraints. | CREATE TABLE Student ( |
| ALTER TABLE | Modify an existing table – add, drop or change columns / constraints. | ALTER TABLE Student |
| DROP TABLE | Delete a table and all its data permanently. | DROP TABLE Student; |
| TRUNCATE TABLE | Remove all rows quickly without dropping the table definition. | TRUNCATE TABLE Enrolment; |
| Constraint | Purpose | SQL syntax |
|---|---|---|
| PRIMARY KEY | Uniquely identifies each row. | StudentID INT PRIMARY KEY |
| UNIQUE | Ensures all values in a column are different. | Email VARCHAR(100) UNIQUE |
| NOT NULL | Disallows NULL values. | Name VARCHAR(50) NOT NULL |
| CHECK | Restricts values to a Boolean expression. | Age INT CHECK (Age >= 16) |
| DEFAULT | Provides a default value when none is supplied. | Phone VARCHAR(15) DEFAULT 'UNKNOWN' |
| FOREIGN KEY … REFERENCES … ON DELETE/UPDATE … | Enforces referential integrity between tables. | FOREIGN KEY (StudentID) |
| Command | Purpose | Example |
|---|---|---|
| SELECT | Retrieve rows that satisfy a condition. | SELECT Name, Age FROM Student WHERE Age > 18; |
| INSERT | Add new rows to a table. | INSERT INTO Student (StudentID, Name, Age) |
| UPDATE | Modify existing rows. | UPDATE Student |
| DELETE | Remove rows from a table. | DELETE FROM Student |
| Feature | SQL syntax | Typical use |
|---|---|---|
| JOIN types | -- INNER (default) | Combine rows from two tables; LEFT shows all students even if they have no enrolments. |
| Aggregate functions | SELECT CourseCode, COUNT(*) AS NumStudents | Summarise data – e.g., number of students per course. |
| DISTINCT | SELECT DISTINCT Semester FROM Enrolment; | Remove duplicate rows. |
| IN / BETWEEN / LIKE | WHERE CourseCode IN ('CS101','MA102') | Various ways to filter rows. |
| SET operations (UNION, INTERSECT, EXCEPT) | SELECT StudentID FROM Enrolment WHERE Semester='Fall21' | Combine results from two queries. |
| Sub‑query in WHERE | SELECT Name FROM Student | Find students enrolled in a particular course. |
The following design satisfies 3NF and demonstrates every DML operation you may be asked to write.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 16),
Phone VARCHAR(15) DEFAULT 'UNKNOWN'
);
CREATE TABLE Enrolment (
EnrolmentID INT PRIMARY KEY,
StudentID INT,
CourseCode VARCHAR(10) NOT NULL,
Semester VARCHAR(6) NOT NULL,
CONSTRAINT FKEnrolStudent
FOREIGN KEY (StudentID)
REFERENCES Student(StudentID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
-- 1. Insert new students
INSERT INTO Student (StudentID, Name, Age, Phone) VALUES
(201, 'Bob', 20, '01234 567890'),
(202, 'Clara', 19, '09876 543210');
-- 2. Enrol the students in courses
INSERT INTO Enrolment (EnrolmentID, StudentID, CourseCode, Semester) VALUES
(301, 201, 'CS101', 'Fall21'),
(302, 202, 'CS101', 'Fall21'),
(303, 202, 'MA102', 'Fall21');
-- 3. Update a student's age (Bob has a birthday)
UPDATE Student
SET Age = Age + 1
WHERE StudentID = 201;
-- 4. Delete an enrolment (Clara drops MA102)
DELETE FROM Enrolment
WHERE EnrolmentID = 303;
-- 5. List all students together with the courses they are currently enrolled in
SELECT s.Name, e.CourseCode, e.Semester
FROM Student s
JOIN Enrolment e ON s.StudentID = e.StudentID
ORDER BY s.Name;
-- 6. Find students who are not enrolled in any course
SELECT s.Name
FROM Student s
LEFT JOIN Enrolment e ON s.StudentID = e.StudentID
WHERE e.StudentID IS NULL;
-- 7. Number of students per course (aggregation)
SELECT CourseCode,
COUNT(*) AS NumStudents
FROM Enrolment
GROUP BY CourseCode
HAVING COUNT(*) > 0
ORDER BY NumStudents DESC;
INSERT adds data to both tables. The foreign key in Enrolment guarantees that only existing StudentID values can be used.UPDATE changes a single row because the WHERE clause uniquely identifies the student.DELETE removes exactly one enrolment; the WHERE clause prevents accidental loss of all rows.INNER JOIN – only students with at least one enrolment appear.LEFT JOIN plus IS NULL finds students with no enrolments.GROUP BY, COUNT and HAVING to produce a summary.The design separates Student (entity data) from Enrolment (relationship data). This achieves:
Enrolment is a single column (EnrolmentID), so no non‑key attribute depends only on part of a composite key.This is the type of model the Cambridge syllabus expects you to recognise and use when writing DML scripts.
NOT NULL or CHECK constraints – leads to illegal or meaningless data.REFERENCES clause – destroys referential integrity.DELETE without a WHERE clause – deletes every row in the table.WHERE clause in UPDATE – may modify many rows unintentionally.ON DELETE/UPDATE CASCADE when you want dependent rows removed or updated automatically.SELECT * in scripts – makes the query fragile if the table definition changes.Student (1) ↔ Enrolment (M). The foreign‑key from Enrolment to Student enforces referential integrity.
Phone to the Student table, and ensure it cannot contain NULL values?ON DELETE CASCADE clause in the foreign‑key definition is useful.GROUP BY and HAVING to show courses with more than three students.CourseCode were stored directly in the Student table.ALTER TABLE Student ADD Phone VARCHAR(15) NOT NULL;SELECT s.NameFROM Student s
LEFT JOIN Enrolment e ON s.StudentID = e.StudentID
WHERE e.StudentID IS NULL;
ON DELETE CASCADE automatically removes all enrolment rows that reference a student when that student is deleted, keeping the database consistent without manual clean‑up.SELECT CourseCode, COUNT(*) AS NumStudentsFROM Enrolment
GROUP BY CourseCode
HAVING COUNT(*) > 3;
CourseCode in Student creates a repeating group (violates 1NF) because a student can take many courses.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.