0
0
PostgreSQLquery~20 mins

NTILE for distribution in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NTILE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Understanding NTILE distribution buckets
Given the table sales with columns id and amount, what is the output of this query?
SELECT id, amount, NTILE(3) OVER (ORDER BY amount) AS bucket FROM sales ORDER BY id;
PostgreSQL
CREATE TABLE sales (id INT, amount INT);
INSERT INTO sales VALUES (1, 100), (2, 200), (3, 300), (4, 400), (5, 500), (6, 600), (7, 700);

SELECT id, amount, NTILE(3) OVER (ORDER BY amount) AS bucket FROM sales ORDER BY id;
A[{"id":1,"amount":100,"bucket":1},{"id":2,"amount":200,"bucket":1},{"id":3,"amount":300,"bucket":2},{"id":4,"amount":400,"bucket":2},{"id":5,"amount":500,"bucket":3},{"id":6,"amount":600,"bucket":3},{"id":7,"amount":700,"bucket":3}]
B[{"id":1,"amount":100,"bucket":1},{"id":2,"amount":200,"bucket":2},{"id":3,"amount":300,"bucket":2},{"id":4,"amount":400,"bucket":3},{"id":5,"amount":500,"bucket":3},{"id":6,"amount":600,"bucket":3},{"id":7,"amount":700,"bucket":3}]
C[{"id":1,"amount":100,"bucket":1},{"id":2,"amount":200,"bucket":1},{"id":3,"amount":300,"bucket":2},{"id":4,"amount":400,"bucket":3},{"id":5,"amount":500,"bucket":3},{"id":6,"amount":600,"bucket":3},{"id":7,"amount":700,"bucket":3}]
D[{"id":1,"amount":100,"bucket":1},{"id":2,"amount":200,"bucket":1},{"id":3,"amount":300,"bucket":1},{"id":4,"amount":400,"bucket":2},{"id":5,"amount":500,"bucket":2},{"id":6,"amount":600,"bucket":3},{"id":7,"amount":700,"bucket":3}]
Attempts:
2 left
💡 Hint
NTILE divides rows into equal groups as evenly as possible, ordered by the specified column.
query_result
intermediate
2:00remaining
NTILE with ties in ordering column
Consider the table scores with columns player and score. What is the output of this query?
SELECT player, score, NTILE(4) OVER (ORDER BY score DESC) AS quartile FROM scores ORDER BY player;
PostgreSQL
CREATE TABLE scores (player TEXT, score INT);
INSERT INTO scores VALUES ('Alice', 90), ('Bob', 80), ('Charlie', 80), ('David', 70), ('Eve', 60), ('Frank', 50), ('Grace', 40), ('Heidi', 30);

SELECT player, score, NTILE(4) OVER (ORDER BY score DESC) AS quartile FROM scores ORDER BY player;
A[{"player":"Alice","score":90,"quartile":1},{"player":"Bob","score":80,"quartile":1},{"player":"Charlie","score":80,"quartile":2},{"player":"David","score":70,"quartile":2},{"player":"Eve","score":60,"quartile":3},{"player":"Frank","score":50,"quartile":3},{"player":"Grace","score":40,"quartile":4},{"player":"Heidi","score":30,"quartile":4}]
B[{"player":"Alice","score":90,"quartile":1},{"player":"Bob","score":80,"quartile":1},{"player":"Charlie","score":80,"quartile":1},{"player":"David","score":70,"quartile":2},{"player":"Eve","score":60,"quartile":2},{"player":"Frank","score":50,"quartile":3},{"player":"Grace","score":40,"quartile":4},{"player":"Heidi","score":30,"quartile":4}]
C[{"player":"Alice","score":90,"quartile":1},{"player":"Bob","score":80,"quartile":2},{"player":"Charlie","score":80,"quartile":2},{"player":"David","score":70,"quartile":2},{"player":"Eve","score":60,"quartile":3},{"player":"Frank","score":50,"quartile":3},{"player":"Grace","score":40,"quartile":4},{"player":"Heidi","score":30,"quartile":4}]
D[{"player":"Alice","score":90,"quartile":1},{"player":"Bob","score":80,"quartile":2},{"player":"Charlie","score":80,"quartile":2},{"player":"David","score":70,"quartile":3},{"player":"Eve","score":60,"quartile":3},{"player":"Frank","score":50,"quartile":4},{"player":"Grace","score":40,"quartile":4},{"player":"Heidi","score":30,"quartile":4}]
Attempts:
2 left
💡 Hint
NTILE assigns rows evenly but does not group ties together; ordering is strict.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in NTILE usage
Which option contains a syntax error when using NTILE in PostgreSQL?
ASELECT id, NTILE(4) OVER (ORDER BY amount) AS quartile FROM sales;
BSELECT id, NTILE() OVER (ORDER BY amount) AS quartile FROM sales;
CSELECT id, NTILE(4) OVER (PARTITION BY category ORDER BY amount) AS quartile FROM sales;
DSELECT id, NTILE(4) OVER (ORDER BY amount DESC) AS quartile FROM sales;
Attempts:
2 left
💡 Hint
NTILE requires a number inside the parentheses.
optimization
advanced
2:00remaining
Optimizing NTILE calculation on large data
You want to assign NTILE(10) buckets on a large table transactions ordered by transaction_date. Which approach is most efficient?
ACreate an index on transaction_date and then use NTILE(10) OVER (ORDER BY transaction_date).
BUse a subquery to first order by transaction_date, then apply NTILE(10) in outer query.
CUse NTILE(10) OVER (ORDER BY transaction_date) directly in the SELECT without indexes.
DCalculate row_number() OVER (ORDER BY transaction_date) and then divide manually into buckets.
Attempts:
2 left
💡 Hint
Indexes help speed up ORDER BY operations used in window functions.
🧠 Conceptual
expert
1:30remaining
NTILE behavior with uneven row counts
If a table has 11 rows and you use NTILE(4), how many rows will be in each bucket?
ABuckets 1 and 2 have 2 rows each; buckets 3 and 4 have 3 rows each.
BBuckets 1 and 2 have 3 rows each; buckets 3 and 4 have 2 rows each.
CBuckets 1, 2, and 3 have 3 rows each; bucket 4 has 2 rows.
DBuckets 1, 2, 3, and 4 all have 3 rows each.
Attempts:
2 left
💡 Hint
NTILE tries to distribute rows as evenly as possible, with earlier buckets getting the extra rows.