← Back to SQL, page 7

sql-0135 · Medium mid

How do you turn rows into columns, for example one row per student with a column per course mark?

Use conditional aggregation: group by the row key and write one MAX(CASE WHEN course_id = 10 THEN marks END) per output column. The CASE keeps only the matching value and NULL elsewhere, and MAX picks it. This works on every engine, unlike vendor PIVOT syntax.

Full answer
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);
sql
SELECT student_id,
       MAX(CASE WHEN course_id = 10 THEN marks END) AS databases,
       MAX(CASE WHEN course_id = 20 THEN marks END) AS algorithms,
       MAX(CASE WHEN course_id = 30 THEN marks END) AS statistics
FROM enrolments
GROUP BY student_id
ORDER BY student_id;
student_iddatabasesalgorithmsstatistics
18892NULL
275NULLNULL
3647180
5NULL55NULL

Each CASE returns the mark for its course and NULL for other rows. MAX ignores NULLs, so it returns the single mark per student, or NULL when the student did not take the course. Ben has a row for Statistics with a NULL mark, which also shows as NULL.

Dara does not appear, because she has no enrolments. Start from students with a LEFT JOIN to include her.

The column list is fixed in the query text. For a dynamic set of columns, generate the SQL in application code, or use vendor features such as SQL Server's PIVOT or PostgreSQL's crosstab from the tablefunc extension (not run here).

PostgreSQL also accepts MAX(marks) FILTER (WHERE course_id = 10), and SQLite 3.30+ supports FILTER too.

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

SELECT student_id,
       MAX(CASE WHEN course_id = 10 THEN marks END) AS databases,
       MAX(CASE WHEN course_id = 20 THEN marks END) AS algorithms,
       MAX(CASE WHEN course_id = 30 THEN marks END) AS statistics
FROM enrolments
GROUP BY student_id
ORDER BY student_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Conditional aggregation pivots rows into columns
  • CASE returns the value for one column and NULL otherwise
  • MAX or SUM collapses each group
  • Columns are fixed; dynamic pivots need generated SQL

Common mistakes

  • Forgetting GROUP BY and getting one row per enrolment
  • Using SUM where a missing value should stay NULL and not become 0 after COALESCE

Follow-ups

  • How would you unpivot columns back into rows?
  • How would you include students with no enrolments?

Tips

  • Explain why MAX is safe here