Quartiles
Question:
We are worried our cats are too fat and need to diet.
We would like to group the cats into quartiles by their weight.
Return: name, weight, weight_quartile
Order by: weight
Show Table
Cats:
| name | varchar |
| breed | varchar |
| weight | float |
| color | varchar |
| age | int |
Show Desired Output
Desired output:
| name | weight | weight_quartile |
| Tigger | 3.80 | 1 |
| Molly | 4.20 | 1 |
| Ashes | 4.50 | 1 |
| Charlie | 4.80 | 2 |
| Smudge | 4.90 | 2 |
| Felix | 5.00 | 2 |
| Puss | 5.10 | 3 |
| Millie | 5.40 | 3 |
| Alfie | 5.50 | 3 |
| Misty | 5.70 | 4 |
| Oscar | 6.10 | 4 |
| Smokey | 6.10 | 4 |
Query Window:
Correct output but can you use 'ntile'?
×
Good work!
ntile() lets us divide our data into percentiles/quartiles.
If we only need to see the lowest X% this can be done via ntile()
next question
Show Answer
Answer:
select
name, weight, ntile(4) over ( order by weight) as weight_quartile
from cats
order by weight