Full answer
Most joins compare keys with =. A non-equi join uses any other condition, such as a range. Grade bands are the classic case, because a mark belongs to the band whose range contains it.
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 grade_bands (grade TEXT PRIMARY KEY, min_mark INTEGER, max_mark INTEGER);
INSERT INTO grade_bands VALUES ('A',85,100),('B',70,84),('C',50,69),('F',0,49);SELECT s.name, e.course_id, e.marks, g.grade
FROM enrolments e
JOIN students s ON s.id = e.student_id
LEFT JOIN grade_bands g ON e.marks BETWEEN g.min_mark AND g.max_mark
ORDER BY s.name, e.course_id;| name | course_id | marks | grade |
|---|---|---|---|
| Asha | 10 | 88 | A |
| Asha | 20 | 92 | A |
| Ben | 10 | 75 | B |
| Ben | 30 | NULL | NULL |
| Chen | 10 | 64 | C |
| Chen | 20 | 71 | B |
| Chen | 30 | 80 | B |
| Eli | 20 | 55 | C |
The LEFT JOIN keeps Ben's Statistics enrolment, whose mark is NULL; it matches no band and shows a NULL grade instead of disappearing.
Range joins depend on clean bands. If two bands overlapped (say B was 60 to 80 and A was 80 to 100), a mark of 80 would match both and the enrolment would appear twice. If bands had a gap, some marks would match nothing. A quick check for overlaps is itself a self join on the band table:
SELECT a.grade, b.grade AS overlaps_with
FROM grade_bands a
JOIN grade_bands b
ON a.grade < b.grade
AND a.min_mark <= b.max_mark
AND b.min_mark <= a.max_mark;| grade | overlaps_with |
|---|
(0 rows)
No rows means no overlaps. For an exclusive upper bound (common with decimal marks such as 79.5), store bands as half-open ranges and join with marks >= min_mark AND marks < next_min.
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 grade_bands (grade TEXT PRIMARY KEY, min_mark INTEGER, max_mark INTEGER);
INSERT INTO grade_bands VALUES ('A',85,100),('B',70,84),('C',50,69),('F',0,49);
SELECT s.name, e.course_id, e.marks, g.grade
FROM enrolments e
JOIN students s ON s.id = e.student_id
LEFT JOIN grade_bands g ON e.marks BETWEEN g.min_mark AND g.max_mark
ORDER BY s.name, e.course_id;
SELECT a.grade, b.grade AS overlaps_with
FROM grade_bands a
JOIN grade_bands b
ON a.grade < b.grade
AND a.min_mark <= b.max_mark
AND b.min_mark <= a.max_mark;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- A non-equi join matches on a range or other condition
- BETWEEN joins assign each value to its band
- Overlapping bands duplicate rows; gaps drop them
- A self join on the band table can check for overlaps
Common mistakes
- Hard-coding grade logic in a long CASE that drifts from the official table
- Using inclusive bounds with decimal values and leaving gaps such as 79.5
Follow-ups
- How would you count students per grade?
- How would you handle decimal marks without gaps?
Tips
- Mention the overlap check; it shows you think about data quality