Bird
Raised Fist0
PostgreSQLquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the NTILE(n) function do in PostgreSQL?
easy
A. It returns the maximum value of a column.
B. It counts the total number of rows in a table.
C. It divides rows into n groups with roughly equal size, numbering each group.
D. It sorts rows alphabetically by a specified column.

Solution

  1. Step 1: Understand NTILE purpose

    NTILE(n) splits rows into n groups based on order.
  2. Step 2: Recognize group numbering

    Each group is numbered starting from 1 to n, distributing rows evenly.
  3. Final Answer:

    It divides rows into n groups with roughly equal size, numbering each group. -> Option C
  4. Quick Check:

    NTILE splits rows into groups [OK]
Hint: NTILE splits rows into equal groups numbered 1 to n [OK]
Common Mistakes:
  • Confusing NTILE with COUNT or MAX functions
  • Thinking NTILE sorts rows alphabetically
  • Assuming NTILE returns row counts
2. Which of the following is the correct syntax to assign quartiles using NTILE in PostgreSQL?
easy
A. SELECT column, NTILE(4) PARTITION BY column FROM table;
B. SELECT column, NTILE OVER (4 ORDER BY column) AS quartile FROM table;
C. SELECT column, NTILE(4) AS quartile FROM table ORDER BY column;
D. SELECT column, NTILE(4) OVER (ORDER BY column) AS quartile FROM table;

Solution

  1. Step 1: Recall NTILE syntax

    The correct syntax is NTILE(n) OVER (ORDER BY column) to assign groups.
  2. Step 2: Check each option

    SELECT column, NTILE(4) OVER (ORDER BY column) AS quartile FROM table; uses correct syntax with NTILE(4) OVER (ORDER BY column). Others misuse OVER clause or omit it.
  3. Final Answer:

    SELECT column, NTILE(4) OVER (ORDER BY column) AS quartile FROM table; -> Option D
  4. Quick Check:

    NTILE needs OVER with ORDER BY [OK]
Hint: NTILE requires OVER and ORDER BY clauses [OK]
Common Mistakes:
  • Omitting OVER clause
  • Placing number inside OVER incorrectly
  • Using PARTITION BY without ORDER BY
3. Given the table scores with column score values: 10, 20, 30, 40, 50, what is the output of this query?
SELECT score, NTILE(3) OVER (ORDER BY score) AS tile FROM scores ORDER BY score;
medium
A. [{10,1}, {20,1}, {30,2}, {40,2}, {50,3}]
B. [{10,1}, {20,2}, {30,2}, {40,3}, {50,3}]
C. [{10,1}, {20,1}, {30,1}, {40,2}, {50,3}]
D. [{10,1}, {20,2}, {30,3}, {40,3}, {50,3}]

Solution

  1. Step 1: Understand NTILE(3) distribution

    NTILE(3) divides 5 rows into 3 groups as evenly as possible: 2 rows in first group, 2 in second, 1 in third.
  2. Step 2: Assign tiles by order

    Ordered scores: 10,20 (tile 1), 30,40 (tile 2), 50 (tile 3).
  3. Final Answer:

    [{10,1}, {20,1}, {30,2}, {40,2}, {50,3}] -> Option A
  4. Quick Check:

    5 rows split into 3 groups: 2,2,1 [OK]
Hint: Divide rows evenly; extra rows go to earlier tiles [OK]
Common Mistakes:
  • Assigning one row per tile ignoring total rows
  • Not ordering rows before applying NTILE
  • Assuming equal rows per tile always
4. Identify the error in this query:
SELECT id, NTILE(4) ORDER BY score FROM results;
medium
A. Missing OVER() clause after NTILE(4).
B. ORDER BY cannot be used inside NTILE function.
C. NTILE requires a PARTITION BY clause.
D. The query is correct as is.

Solution

  1. Step 1: Check NTILE syntax

    NTILE requires an OVER() clause with ORDER BY inside it, like NTILE(4) OVER (ORDER BY score).
  2. Step 2: Identify missing OVER()

    The query misses the OVER() clause, causing syntax error.
  3. Final Answer:

    Missing OVER() clause after NTILE(4). -> Option A
  4. Quick Check:

    NTILE needs OVER() with ORDER BY [OK]
Hint: NTILE always needs OVER() with ORDER BY [OK]
Common Mistakes:
  • Placing ORDER BY outside OVER()
  • Forgetting OVER() clause
  • Assuming PARTITION BY is mandatory
5. You want to divide 7 employees into 3 salary groups using NTILE, but the salaries are:
5000, 7000, 7000, 9000, 11000, 11000, 13000.
Which NTILE(3) group assignment is correct when ordered by salary?
hard
A. [{5000,1}, {7000,1}, {7000,1}, {9000,2}, {11000,2}, {11000,3}, {13000,3}]
B. [{5000,1}, {7000,1}, {7000,2}, {9000,3}, {11000,3}, {11000,3}, {13000,3}]
C. [{5000,1}, {7000,2}, {7000,2}, {9000,2}, {11000,3}, {11000,3}, {13000,3}]
D. [{5000,1}, {7000,1}, {7000,2}, {9000,2}, {11000,3}, {11000,3}, {13000,3}]

Solution

  1. Step 1: Calculate group sizes for 7 rows and 3 groups

    7 rows divided into 3 groups: first group gets 3 rows, next two get 2 rows each.
  2. Step 2: Assign groups by ordered salary

    Order: 5000(1), 7000(1), 7000(2), 9000(3), 11000(3), 11000(3), 13000(3).
  3. Final Answer:

    [{5000,1}, {7000,1}, {7000,2}, {9000,3}, {11000,3}, {11000,3}, {13000,3}] -> Option B
  4. Quick Check:

    7 rows split into 3 groups: 3,2,2 [OK]
Hint: Extra rows go to earlier groups; order matters [OK]
Common Mistakes:
  • Ignoring duplicate salaries affecting group boundaries
  • Assigning equal rows per group ignoring total rows
  • Misplacing group numbers for duplicates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard