SQL Window Functions: Ranking, Top N per Group, and Percent Rank/Split rows into quartiles with NTILE
Bucket ordered rows into four groups.
Section: Ranking functions
Split rows into quartiles with NTILE
sql
sql
SELECT customer_id,
lifetime_value,
NTILE(4) OVER (ORDER BY lifetime_value DESC) AS quartile
FROM customers;Explanation
`NTILE` is handy for quick segmentation such as quartiles or deciles.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Ranking functions