Bird
0
0

How can you use NTILE to assign deciles to a dataset and then filter only the top 10% of rows?

hard📝 Application Q9 of 15
PostgreSQL - Window Functions in PostgreSQL
How can you use NTILE to assign deciles to a dataset and then filter only the top 10% of rows?
AUse NTILE(10) OVER (ORDER BY value DESC) and filter where bucket = 1
BUse NTILE(100) OVER (ORDER BY value) and filter where bucket <= 10
CUse NTILE(10) OVER (ORDER BY value) and filter where bucket = 10
DUse NTILE(10) OVER (ORDER BY value) and filter where bucket = 1
Step-by-Step Solution
Solution:
  1. Step 1: Assign deciles with NTILE(10)

    NTILE(10) splits data into 10 groups ordered by value.
  2. Step 2: Filter top 10%

    Ordering DESC puts highest values first; bucket 1 is top 10%.
  3. Final Answer:

    Use NTILE(10) OVER (ORDER BY value DESC) and filter where bucket = 1 -> Option A
  4. Quick Check:

    Top decile = bucket 1 when ordering DESC [OK]
Quick Trick: Order DESC to get top values in bucket 1 [OK]
Common Mistakes:
  • Filtering bucket 10 instead of bucket 1 for top values
  • Ordering ASC and expecting bucket 1 to be top

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes