← Back to SQL, page 3

sql-0057 · Medium mid

Write a query that assigns a grade to each mark using a grade-band table with min and max marks.

Join marks to the band table with a range condition instead of equality: ON e.marks BETWEEN g.min_mark AND g.max_mark. This is a non-equi join. Make sure the bands do not overlap or leave gaps, or a mark can match two grades or none; a LEFT JOIN exposes marks that fall into no band.

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.

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 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);
sql
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;
namecourse_idmarksgrade
Asha1088A
Asha2092A
Ben1075B
Ben30NULLNULL
Chen1064C
Chen2071B
Chen3080B
Eli2055C

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:

sql
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;
gradeoverlaps_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.

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