← Back to SQL, page 7

sql-0122 · Medium mid

How do you split students into four equal-sized groups by marks? Explain NTILE.

NTILE(4) OVER (ORDER BY marks) deals rows into 4 buckets as evenly as possible, in order. If the row count is not divisible by 4, the first buckets get one extra row. NTILE splits by row count, not by value, so tied marks can land in different buckets.

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, course_id, marks,
       NTILE(4) OVER (ORDER BY marks DESC, student_id, course_id) AS quartile
FROM enrolments
WHERE marks IS NOT NULL
ORDER BY marks DESC, student_id, course_id;
student_idcourse_idmarksquartile
120921
110881
330802
210752
320713
310643
520554

Seven marks into four buckets: 7 = 2 + 2 + 2 + 1, so buckets 1 to 3 get two rows and bucket 4 gets one. NTILE always assigns by position in the sorted order.

That has a consequence with ties. If two students had the same mark at a bucket boundary, they could be placed in different buckets, which looks unfair in a report. For value-based groups, compute boundaries from the values instead, for example with PERCENT_RANK or fixed cut-offs in a CASE.

The NULL mark is excluded by the WHERE clause. Left in, it would sort first in SQLite (NULLs sort low) and take a place in bucket 1.

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, course_id, marks,
       NTILE(4) OVER (ORDER BY marks DESC, student_id, course_id) AS quartile
FROM enrolments
WHERE marks IS NOT NULL
ORDER BY marks DESC, student_id, course_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • NTILE(n) deals rows into n buckets by position
  • Extra rows go to the first buckets
  • Ties can be split across buckets
  • Exclude or place NULLs deliberately

Common mistakes

  • Calling NTILE buckets 'percentiles' of value
  • Leaving NULLs in and having them fill the first bucket

Follow-ups

  • How would you label the top 25 percent of students?
  • How is PERCENT_RANK different from NTILE?

Tips

  • Do the 7 = 2 + 2 + 2 + 1 arithmetic out loud