0
0
PostgreSQLquery~10 mins

NTILE for distribution in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NTILE for distribution
Start with dataset
Order rows by specified column
Divide rows into N buckets
Assign bucket number to each row
Return rows with bucket number
End
NTILE divides ordered rows into a specified number of groups (buckets) and assigns each row a bucket number.
Execution Sample
PostgreSQL
SELECT id, score, NTILE(3) OVER (ORDER BY score DESC) AS bucket
FROM students;
This query divides students into 3 groups based on their score, assigning each a bucket number.
Execution Table
RowidscoreOrdered PositionBucket NumberAction
11019511First row, assigned to bucket 1
21029021Second row, bucket 1
31038532Third row, bucket 2
41048042Fourth row, bucket 2
51057553Fifth row, bucket 3
61067063Sixth row, bucket 3
71076573Seventh row, bucket 3
ExitAll rows assigned to one of 3 buckets
💡 All 7 rows are divided into 3 buckets; buckets 1 and 2 have 2 rows each, bucket 3 has 3 rows.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5After Row 6After Row 7Final
Bucket NumberN/A1122333Final bucket assignments per row
Key Moments - 2 Insights
Why do some buckets have more rows than others?
Because the total number of rows (7) is not divisible evenly by the number of buckets (3), some buckets get one extra row. See execution_table rows 5-7 where bucket 3 has 3 rows.
How does NTILE decide which rows go to which bucket?
NTILE orders rows by the specified column (score DESC here), then divides them into buckets in order. The first rows get bucket 1, next bucket 2, and so on, as shown in the Ordered Position and Bucket Number columns.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what bucket number is assigned to the row with id 103?
A1
B3
C2
DNone
💡 Hint
Check the row where id is 103 in the execution_table and see the Bucket Number column.
At which row does the bucket number change from 1 to 2?
ARow 3
BRow 2
CRow 4
DRow 5
💡 Hint
Look at the Bucket Number column in execution_table and find where it changes from 1 to 2.
If we change NTILE(3) to NTILE(2), how many buckets will the rows be divided into?
A3
B2
C7
D1
💡 Hint
NTILE(N) always divides rows into N buckets, so changing 3 to 2 means 2 buckets.
Concept Snapshot
NTILE(n) divides ordered rows into n groups (buckets).
Rows are assigned bucket numbers from 1 to n.
If rows don't divide evenly, some buckets get one more row.
Use OVER(ORDER BY column) to specify order.
Useful for ranking or distributing data evenly.
Full Transcript
NTILE is a function in SQL that splits rows into a specified number of groups called buckets. First, it orders the rows by a chosen column. Then it divides the rows into equal or nearly equal buckets. Each row gets a bucket number from 1 up to the number of buckets. If the total rows don't divide evenly, some buckets have one extra row. For example, with 7 rows and NTILE(3), buckets 1 and 2 get 2 rows each, and bucket 3 gets 3 rows. This helps distribute data evenly for analysis or reporting.