SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Ranking Functions – Advanced NTILE in Detail – Day 13 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 2.
Every day one winner from United States will get Joes 2 Pros Volume 2.

Ranking Functions Part 2 (NTILE)

A friend of mine recently told me she’s very proud of her son, because he is consistently in the upper quarter of every class he takes. Right there she performed a calculation similar to the NTILE function. She didn’t know it, but she tiled the class into four pieces and then identified which piece her son belongs in.

Just like its three fellow ranking functions, NTILE takes data you’ve sorted and then marches straight down the list in order of row number. Instead of assigning a rank value to each record, NTILE counts records and assigns them into a group (a.k.a., “tile”, or “bucket” in geek speak) based on:
1) the number of tiles/pieces you have specified, and
2) the count of records contained in the query.

Like all other ranking functions, we want to first write a SELECT statement and an ORDER BY clause to determine the sort order.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Ranking Functions - Advanced NTILE in Detail - Day 13 of 35 j2p_13_1

Adding the NTILE function to the query is almost the same as with the first three ranking functions we saw from yesterdays blog post. The ORDER BY clause gets plugged into the parentheses of the OVER( ) clause. Just one new item to note with the NTILE:  you must insert the number of tiles (or pieces) in the parentheses of the NTILE. In this case we want four pieces, just like my friend’s story about her son.

The upper quarter consists of Grants 007, 008, 009 and are the “1” group of records. The lowest quarter (the group “4” records) consists of Grants 001 and 010 as seen in the figure below

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Ranking Functions - Advanced NTILE in Detail - Day 13 of 35 j2p_13_2

If you want just an upper and lower half ranking just change this query to show just two pieces. We see the grant records divided into an upper half (the “1” records) and a lower half (“2” records). The figure below shows the top 5 grants in group 1 and the lowest 5 grants in group 2. In this case the NTILE query now divides the grants into an upper half and lower half.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Ranking Functions - Advanced NTILE in Detail - Day 13 of 35 j2p_13_3

Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLQueriesChapter7.0Setup.sql script from Volume 2.

Question 13

You want to find the top 2% of all students’ Grade Point Averages (GPA). Which NTILE would you use?

  1. NTILE(1) OVER(ORDER BY GPA DESC)
  2. NTILE(2) OVER(ORDER BY GPA DESC)
  3. NTILE(25) OVER(ORDER BY GPA DESC)
  4. NTILE(50) OVER(ORDER BY GPA DESC)

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 2.
Winner from India will get Joes 2 Pros Volume 2.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Ranking Functions – RANK( ), DENSE_RANK( ), and ROW_NUMBER( ) – Day 12 of 35
Next Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Output Clause in Simple Examples – Day 14 of 35

Related Posts

56 Comments. Leave new

  • Shatrughna Kumar
    August 13, 2011 7:33 am

    Correct answer is option 2.
    New Delhi

    Reply
  • For this question: Question 13

    You want to find the top 2% of all students’ Grade Point Averages (GPA). Which NTILE would you use?

    I believe the answer is
    4. NTILE(50) OVER(ORDER BY GPA DESC)

    Dawn (USA)

    Reply
  • Rene Alberto Castro Velasquez
    August 13, 2011 7:41 am

    Correct answer is No. 4, because each tile contains 2% of all students’ Grade Point Averages (GPA).
    Rene Castro
    El Salvador

    Reply
  • 4) NTILE(50) OVER(ORDER BY GPA DESC)

    2/100 = 1/50
    So we need 50 parts so each part would represent 2% of the students

    Reply
  • Ramakrishnan Srinivasan
    August 13, 2011 8:44 am

    We are looking at 2% – 1/50th of the entire populace. This needs 50 tiles to be created. Hence, option 4 (NTILE(50)) is appropriate.
    Ramakrishnan RS
    Mysore, India

    Reply
  • correct answer is:(4) NTILE(50) OVER(ORDER BY GPA DESC)
    kkmishra
    india

    Reply
  • Aditya Bisoi (@AdityaBisoi07)
    August 13, 2011 9:55 am

    Question 13

    4- NTILE(50) OVER(ORDER BY GPA DESC)

    Chennai, INDIA

    Reply
  • Nakul Vachhrajani
    August 13, 2011 10:22 am

    The correct answer is #4 – i.e. to use NTILE(50) OVER (ORDER BY GPA DESC)

    Reason: I don’t have a database to work this out, but based on pure deduction, 1/50 = 0.02, i.e. 2%.

    Country of residence: India

    Reply
  • Correct answer:4

    NTILE(50) OVER(ORDER BY GPA DESC)

    INDIA
    Brijesh

    Reply
  • Correct Option is 4.

    Shilpa
    India

    Reply
  • Option 4 is Correct

    4. NTILE(50) OVER(ORDER BY GPA DESC)

    India

    Reply
  • The Correct Answer is : 4 (NTILE(50) OVER(ORDER BY GPA DESC))

    — GVPrabu
    Bangalore || India

    Reply
  • Correct Answer is : Option 4

    Because 2% means 100/2 Parts ie..50.
    So 4- NTILE(50) OVER(ORDER BY GPA DESC) is correct Answer

    — GVPrabu
    Bangalore || India

    Reply
  • Basavaraj Biradar
    August 13, 2011 2:17 pm

    Correct Answer is:4

    Thanks,
    Basavaraj
    India

    Reply
  • option 4

    Reply
  • Hi,

    Option 4 is correct.

    Regards,

    Sudhir Chawla
    New Delhi, India

    Reply
  • Correct Answer is : Option 4

    (Sale, Nigeria)

    Reply
  • Kalyanasundaram.K
    August 13, 2011 3:53 pm

    Blog Question have correct option :4

    NTILE(50) OVER(ORDER BY GPA DESC)

    It will be get the top 2 in a piece.

    Chennai, TamilNadu, India

    Reply
  • Anish Shenoy.P
    August 13, 2011 4:03 pm

    Hi Sir,

    The correct answer is Option no 4

    Option 1 :
    NTILE(1) OVER(ORDER BY GPA DESC)

    Will group all the rows in a single group. i.e 100%

    Option 2 :
    NTILE(2) OVER(ORDER BY GPA DESC)

    Will group all the rows in two groups. i.e two groups of 50% each

    Option 3:
    NTILE(25) OVER(ORDER BY GPA DESC)

    Will group all the rows in 25 groups i.e 25 groups of 4% each.

    Option 4:
    NTILE(50) OVER(ORDER BY GPA DESC)

    Will group all the rows in 50 groups i.e 50 groups of 2% each.

    so after this query the first group is the top 2% of all students’ Grade Point Averages (GPA).

    So the correct option is 4.

    P.Anish Shenoy,
    INDIA

    Reply
  • Hi All,

    Correct answer is 4 as bellow

    NTILE(50) OVER(ORDER BY GPA DESC)

    I am from India

    Reply

Leave a Reply