RANK() returns consecutive integers

  • Comments posted to this topic are about the item RANK() returns consecutive integers

  • There are several ranking functions:

    RANK

    DENSE_RANK

    NTILE

    ROW_NUMBER

    When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row. One row is still a non empty rowset but that was not explained in the provided explanation.

  • Nice and easy. Thanks!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • cengland0 (1/6/2011)


    When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row.

    Meh, I could just as well say that everything is consecutive in a single-row rowset.

    According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

    The reason that RANK does not return consecutive values is how ties are handled - for instance, if the RANK is based on the scores {9.6, 8.4, 9.6}, the results would be

    Score Rank

    9.6 1

    9.6 1

    8.4 3

    Because two rows get the rank 1, the value 2 is skipped. That makes the values non-consecutive.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (1/6/2011)


    cengland0 (1/6/2011)


    Meh, I could just as well say that everything is consecutive in a single-row rowset.

    According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

    You forgot a major component of the definition of consecutive. The "one after another" part. When you have a one row rowset, how can you have one after another in the resultset? That means you must have a minimum of two rows to have one after another.

  • cengland0 (1/6/2011)


    Hugo Kornelis (1/6/2011)


    cengland0 (1/6/2011)


    Meh, I could just as well say that everything is consecutive in a single-row rowset.

    According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

    You forgot a major component of the definition of consecutive. The "one after another" part. When you have a one row rowset, how can you have one after another in the resultset? That means you must have a minimum of two rows to have one after another.

    If you really want to take it to a filosophical level, you could argue that one row comes after nothing.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • cengland0 (1/6/2011)


    There are several ranking functions:

    RANK

    DENSE_RANK

    NTILE

    ROW_NUMBER

    Yes, there are several. However, this question is about the RANK() function, so why even bring these others into the mix?

    When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row. One row is still a non empty rowset but that was not explained in the provided explanation.

    Are you just trying to find fault with the question?

    A better argument would have been that all of the windowing functions accept a PARTITION BY clause, which re-starts the numbering - thus the results are not always sequential.

    However, sometimes you just have to go with what you know the author meant to convey.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • WayneS (1/6/2011)


    Yes, there are several. However, this question is about the RANK() function, so why even bring these others into the mix?

    Because I usually have to look them up to make sure I'm using the right one. But in this case, I didn't have to look anything up because none of them would return consecutive integers (when there is a one-row result).

    Are you just trying to find fault with the question?

    Not necessarily. I do work for a very large company, in the training organization, and would consider myself an expert on quizzes.

    There have been cases of companies firing employees during training for failing tests. To avoid legal issues, you better look at each question carefully to make sure the answer they selected can never be a correct answer. For this reason, I am always looking at these QOTD's with a different eye than a regular person.

    In a previous company, I have been involved in the process of firing and disciplinary actions of employees not passing tests. I've also had to gather supporting documentation to take to court when an ex-employee took legal action.

  • Another way to put this is if the question said:

    In a non-empty rowset, ROW_NUMBER() always returns consecutive integers.

    What would you answer? True or False?

    I'd still answer False because it's possible to have a one-row rowset but the author is probably thinking True.

    I did get the answer for this QOTD correct.

  • cengland0 (1/6/2011)


    Another way to put this is if the question said:

    In a non-empty rowset, ROW_NUMBER() always returns consecutive integers.

    What would you answer? True or False?

    I'd still answer False because it's possible to have a one-row rowset but the author is probably thinking True.

    I guess I'd first check to see if you are the author of that question 😉

    But had I seen such a question before today, then I think there's about 80% chance I'd have replied True.

    If the correct answer had been False because of PARTITION BY, I would have slapped myself on the forehead and complimented the question's author.

    If the correct answer had been False because of a single-row rowset "not being consecutive", I would have felt misled and cheated.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • cengland0 (1/6/2011)


    There are several ranking functions:

    . . . One row is still a non empty rowset but that was not explained in the provided explanation.

    You are absolutely right. Thank you for pointing out this boundary case.

    In my defense, I am still doing better than SQLS BOL which are not mentioning an empty set.

    😉

  • Thanks for the question!

  • Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Is it considered cheating that I went to the BOL before answering the question and read "If two or more rows tie for a rank, each tied rows receives the same rank."?

    ______________________________________________________________________________
    How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.

  • toddasd (1/6/2011)


    Is it considered cheating that I went to the BOL before answering the question and read "If two or more rows tie for a rank, each tied rows receives the same rank."?

    I don't consider doing research and learning from it cheating.

    I'm not going to memorize every feature, trace flag, syntax, dmv of SQL Server. If I need something, I'll look it up 🙂

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply