0
0
SQLquery~20 mins

NTILE for distribution in SQL - 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 with 3 buckets
Given the table Sales with a column amount, what will be the NTILE value for the row with amount = 50 when using NTILE(3) OVER (ORDER BY amount)?
SQL
CREATE TABLE Sales(amount INT);
INSERT INTO Sales VALUES (10), (20), (30), (40), (50), (60), (70);

SELECT amount, NTILE(3) OVER (ORDER BY amount) AS bucket FROM Sales ORDER BY amount;
A1
B2
C3
DError
Attempts:
2 left
💡 Hint
NTILE divides rows into equal groups ordered by the column.
🧠 Conceptual
intermediate
1:30remaining
What does NTILE do in SQL?
Which of the following best describes what the SQL function NTILE(n) does when used as a window function?
ADivides rows into <code>n</code> groups with equal number of rows as much as possible, ordered by the specified column.
BCalculates the cumulative sum of values divided by <code>n</code>.
CReturns the rank of each row within its partition.
DCounts the total number of rows in the table.
Attempts:
2 left
💡 Hint
Think about how NTILE splits rows into groups.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in NTILE usage
Which option contains a syntax error when using NTILE in a SQL query?
SQL
SELECT employee_id, NTILE(4) OVER (ORDER BY salary) AS quartile FROM Employees;
ASELECT employee_id, NTILE(4) OVER (ORDER BY salary) AS quartile FROM Employees;
BSELECT employee_id, NTILE(4) OVER (ORDER BY salary DESC) AS quartile FROM Employees;
CSELECT employee_id, NTILE(4) OVER (PARTITION BY department ORDER BY salary) AS quartile FROM Employees;
DSELECT employee_id, NTILE(4) AS quartile FROM Employees ORDER BY salary;
Attempts:
2 left
💡 Hint
NTILE requires an OVER clause with ORDER BY.
query_result
advanced
2:00remaining
NTILE with ties in ordering column
Given the table Scores with values (100, 90, 90, 80, 70) in column score, what is the NTILE(2) value for the rows with score 90 when using NTILE(2) OVER (ORDER BY score DESC)?
SQL
CREATE TABLE Scores(score INT);
INSERT INTO Scores VALUES (100), (90), (90), (80), (70);

SELECT score, NTILE(2) OVER (ORDER BY score DESC) AS bucket FROM Scores ORDER BY score DESC;
AError
B2
C1
DBoth 1 and 2
Attempts:
2 left
💡 Hint
NTILE assigns buckets based on row position, ties do not get same bucket necessarily.
optimization
expert
3:00remaining
Optimizing NTILE on large datasets
You have a large table Transactions with millions of rows. You want to assign quartiles using NTILE(4) OVER (ORDER BY amount). Which approach will improve performance the most?
ACreate an index on the <code>amount</code> column before running the NTILE query.
BUse a subquery to pre-sort the data and then apply NTILE on the result.
CAdd a WHERE clause filtering only the top 1000 rows before applying NTILE.
DRun NTILE without any ORDER BY clause to avoid sorting overhead.
Attempts:
2 left
💡 Hint
Think about how sorting affects window functions.