0
0
PostgreSQLquery~5 mins

NTILE for distribution in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the NTILE function do in SQL?
NTILE divides rows in a result set into a specified number of groups, assigning a group number to each row to show its distribution.
Click to reveal answer
intermediate
How does NTILE assign group numbers when the rows don't divide evenly?
NTILE assigns extra rows to the first groups so that groups at the start have one more row than later groups.
Click to reveal answer
beginner
Write a simple NTILE query to split 10 rows into 3 groups ordered by salary.
SELECT employee_id, salary, NTILE(3) OVER (ORDER BY salary) AS group_number FROM employees;
Click to reveal answer
beginner
Why is the ORDER BY clause important in the NTILE function?
ORDER BY defines how rows are sorted before being divided into groups, affecting which rows fall into each group.
Click to reveal answer
intermediate
Can NTILE be used without an ORDER BY clause? What happens?
No, NTILE requires ORDER BY to define row order; without it, the function will cause an error.
Click to reveal answer
What does NTILE(4) do to a result set?
ADivides rows into 4 groups with group numbers assigned
BSorts rows in descending order
CCounts total rows in the table
DFilters rows with values less than 4
If you have 10 rows and use NTILE(3), how many rows will the first group have?
A2
B3
C4
D5
Which clause must be used with NTILE to define row order?
AGROUP BY
BORDER BY
CWHERE
DHAVING
What type of SQL function is NTILE?
AString function
BAggregate function
CScalar function
DWindow function
What happens if you use NTILE(1) on a result set?
AAll rows get group number 1
BError occurs
CEach row gets a unique group number
DRows are split into two groups
Explain how NTILE distributes rows into groups and how it handles uneven division.
Think about how you would split a class into teams when the number of students isn't divisible evenly.
You got /3 concepts.
    Describe a real-life scenario where NTILE could be useful in analyzing data.
    Consider how businesses or schools categorize people based on rankings or scores.
    You got /3 concepts.