Full answer
A composite key identifies a row by several columns together. An enrolment is "this student in this course", not just "this student".
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
CREATE TABLE moderation (student_id INTEGER, course_id INTEGER, adjustment INTEGER,
PRIMARY KEY (student_id, course_id));
INSERT INTO moderation VALUES (1, 10, 2), (1, 20, -1), (3, 10, 5);The moderation table records mark adjustments per enrolment:
SELECT * FROM moderation ORDER BY student_id, course_id;| student_id | course_id | adjustment |
|---|---|---|
| 1 | 10 | 2 |
| 1 | 20 | -1 |
| 3 | 10 | 5 |
Joining on half of the key:
SELECT e.student_id, e.course_id, e.marks, m.course_id AS mod_course, m.adjustment
FROM enrolments e
JOIN moderation m ON m.student_id = e.student_id
WHERE e.student_id = 1
ORDER BY e.course_id, m.course_id;| student_id | course_id | marks | mod_course | adjustment |
|---|---|---|---|---|
| 1 | 10 | 88 | 10 | 2 |
| 1 | 10 | 88 | 20 | -1 |
| 1 | 20 | 92 | 10 | 2 |
| 1 | 20 | 92 | 20 | -1 |
Asha's two enrolments matched both of her moderation rows, so she has four rows instead of two, and her Algorithms mark is shown with the Databases adjustment as well. Any SUM over this result is wrong.
Joining on the whole key:
SELECT e.student_id, e.course_id, e.marks, m.adjustment, e.marks + m.adjustment AS final
FROM enrolments e
JOIN moderation m ON m.student_id = e.student_id AND m.course_id = e.course_id
ORDER BY e.student_id, e.course_id;| student_id | course_id | marks | adjustment | final |
|---|---|---|---|---|
| 1 | 10 | 88 | 2 | 90 |
| 1 | 20 | 92 | -1 | 91 |
| 3 | 10 | 64 | 5 | 69 |
A quick check for this class of bug: compare the row count of the join with the row count of the table you expected to drive it. If joining enrolments to a lookup returns more rows than enrolments has (with the same filter), the join key is incomplete or the lookup has duplicates.
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
CREATE TABLE moderation (student_id INTEGER, course_id INTEGER, adjustment INTEGER,
PRIMARY KEY (student_id, course_id));
INSERT INTO moderation VALUES (1, 10, 2), (1, 20, -1), (3, 10, 5);
SELECT * FROM moderation ORDER BY student_id, course_id;
SELECT e.student_id, e.course_id, e.marks, m.course_id AS mod_course, m.adjustment
FROM enrolments e
JOIN moderation m ON m.student_id = e.student_id
WHERE e.student_id = 1
ORDER BY e.course_id, m.course_id;
SELECT e.student_id, e.course_id, e.marks, m.adjustment, e.marks + m.adjustment AS final
FROM enrolments e
JOIN moderation m ON m.student_id = e.student_id AND m.course_id = e.course_id
ORDER BY e.student_id, e.course_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- A composite key needs every column in the join condition
- Joining on part of the key multiplies rows
- Wrong pairings attach data to the wrong entity, not only duplicates
- Compare row counts before and after a join as a sanity check
Common mistakes
- Joining on student_id only because it 'looks like the id'
- Fixing the duplicates with DISTINCT and keeping wrong pairings
Follow-ups
- How would you enforce that each enrolment has at most one moderation row?
- What is a surrogate key, and would it help here?
Tips
- Name the row-count check; it catches this bug in any query