Interview Q&A / SQL & DBMS

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What does NTILE do and where is it used?

SHORT ANSWER

NTILE(n) divides the ordered rows into n roughly equal buckets and returns the bucket number for each row. It is used for quartiles, percentiles and splitting data into groups.

Explain it simply

Line up the class by marks and split it into four equal queues: top quarter, second quarter and so on. NTILE(4) stamps each student with the queue number 1 to 4.

Example

Eight students ranked by marks:

sql
SELECT name, marks,
       NTILE(4) OVER (ORDER BY marks DESC) AS quartile
FROM students;

With 8 rows, each bucket has 2 rows: the top two get 1, the next two get 2, and so on.

If rows do not divide evenly, earlier buckets get one extra row. For 10 rows and NTILE(4), the bucket sizes are 3, 3, 2, 2.

Typical uses:

  • Top 25 percent customers by spend.
  • Splitting users into A/B test groups.
  • Quick percentile bands for salaries.

NTILE only balances row counts, not values. Two equal marks can land in different buckets, since ties are not kept together. For tie-aware percentages use PERCENT_RANK or CUME_DIST.

What to say in the interview

"NTILE(n) assigns bucket numbers 1 to n over the ordered window, with sizes differing by at most one row. It splits by row count and ignores ties."

Common mistakes

  • Assuming equal values always share a bucket.

#sql#window-functions#ntile

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.