← Back to SQL, page 4

sql-0070 · Medium mid

An enrolments table has a composite key (student_id, course_id). What goes wrong if you join it to a moderation table on student_id only?

Each enrolment then matches every moderation row for that student, across all their courses, so rows multiply and marks get adjustments that belong to other courses. Join on every column of the composite key: ON m.student_id = e.student_id AND m.course_id = e.course_id.

Full answer

A composite key identifies a row by several columns together. An enrolment is "this student in this course", not just "this student".

sql
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:

sql
SELECT * FROM moderation ORDER BY student_id, course_id;
student_idcourse_idadjustment
1102
120-1
3105

Joining on half of the key:

sql
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_idcourse_idmarksmod_courseadjustment
11088102
1108820-1
12092102
1209220-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:

sql
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_idcourse_idmarksadjustmentfinal
11088290
12092-191
31064569

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.

sql
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