← Back to SQL, page 5

sql-0099 · Medium mid

How do you filter rows using a list of pairs, such as specific (student_id, course_id) combinations, in one condition?

Use a row value comparison: WHERE (student_id, course_id) IN (VALUES (1, 10), (3, 30)) or IN (SELECT ...). SQLite (since 3.15), PostgreSQL and MySQL support row values. Without them, you write OR-ed pairs of conditions or join to a small table of pairs.

Full answer

A row value is a list of columns treated as one value. Comparing row values compares the columns together, which is exactly what a composite key needs.

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);
INSERT INTO moderation VALUES (1, 10), (3, 30);
sql
SELECT student_id, course_id, marks
FROM enrolments
WHERE (student_id, course_id) IN (VALUES (1, 10), (3, 30))
ORDER BY student_id, course_id;
student_idcourse_idmarks
11088
33080

Comparing the columns separately is a common wrong version:

sql
SELECT student_id, course_id, marks
FROM enrolments
WHERE student_id IN (1, 3) AND course_id IN (10, 30)
ORDER BY student_id, course_id;
student_idcourse_idmarks
11088
31064
33080

student_id IN (1, 3) AND course_id IN (10, 30) allows any mix of the two lists, so Chen's Databases enrolment (3, 10) matches too, giving three rows instead of the two requested pairs.

The same row value form works with a subquery, for example to find enrolments that have a moderation record:

sql
SELECT e.student_id, e.course_id, e.marks
FROM enrolments e
WHERE (e.student_id, e.course_id) IN (SELECT m.student_id, m.course_id FROM moderation m)
ORDER BY e.student_id, e.course_id;
student_idcourse_idmarks
11088
33080

Row values also simplify keyset pagination, where (order_date, id) > (?, ?) means "after this position" in one comparison.

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);
INSERT INTO moderation VALUES (1, 10), (3, 30);

SELECT student_id, course_id, marks
FROM enrolments
WHERE (student_id, course_id) IN (VALUES (1, 10), (3, 30))
ORDER BY student_id, course_id;

SELECT student_id, course_id, marks
FROM enrolments
WHERE student_id IN (1, 3) AND course_id IN (10, 30)
ORDER BY student_id, course_id;

SELECT e.student_id, e.course_id, e.marks
FROM enrolments e
WHERE (e.student_id, e.course_id) IN (SELECT m.student_id, m.course_id FROM moderation m)
ORDER BY e.student_id, e.course_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Row values compare several columns as one unit
  • (a, b) IN (VALUES ...) matches exact pairs
  • Separate IN lists allow wrong combinations
  • Row values also power keyset pagination

Common mistakes

  • Using two independent IN lists for a pair filter
  • Concatenating columns into strings to compare pairs

Follow-ups

  • How would you write this on an engine without row values?
  • How does (a, b) > (x, y) compare?

Tips

  • Show the wrong two-list version; the extra row makes the point