T-SQL Logic

  • Comments posted to this topic are about the item T-SQL Logic

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • simple one, just need to analyse the Query.

  • I hope it is not considered cheating that I did run SELECT database_id, name FROM sys.databases ORDER BY 1; before answering. I don't thik there is any need for me to know ids of system databases by head.

    Other than that, the question was a bit too simple. Just the AND database_id < 2 was sufficient to determine that there would be one result, and there was only one option with one result, so there was no need at all to evaluate the CASE and POWER expressions.

    Anyway, thanks for the question Henrico! Your first, I believe? Hopefully there will be more in the future!


    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/

  • and database_id < 2

    this was enough to answer the question...:-)

  • Hi Hugo, yes, looking at the last where statement, it was easy to determine the answer.

    But, look at the wrong answer rate - 19 so far.

    This question was purely to test logic when it comes to reading code, and was not intended to be a massive brain cracker 😀

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • I just used the len(case...) statement - since this can return only one row (master) then the answer is the one with only a single option.

  • It would have been more interesting if it had read "and database_id <= 2

    " which would have brought the case statement into play more.

    That's the way I read it anyway. Good thing it had the same answer 🙂

  • Another easy point 🙂

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • I got this right! I learned my lesson from the last time - after equivocating for a minute, wondering "the choice of 1 is too easy, maybe there's a trick" - I decided that no trick was evident based on the code.

    Simple question for many at SSC, I suppose. But still worth looking at and being able to explain for "lesser experts" (i.e., those of us who get too many QOTD questions wrong :-))

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • And the point is?... lost on me... again.:w00t:

  • there are no negative database ids 🙂

    and there is no 0

    thus 1 is the answer to the most obfuscated question ever!

  • Yeah, the question is overly complicated, and the answer is overly simple. The POWER() function is totally unneccessary, its fairly obvious from the WHERE clause that there's only going to be one row result. Not to mention the WHERE clause is redundant, as both branches will select the master database and only that database.

    Really, I don't think it's a good question. It's complicated apparently just for the sake of being complicated. Save for learning that the database_id of the master database is always 1, no one will really actually bring anything away from this question.

    --J

  • I think it's a good question - designed to make you think a bit and work out that you don't actually need to know anything about database ids in order to get it right.

  • Nice question. Thanks.

    Converting oxygen into carbon dioxide, since 1955.
  • ziangij (6/22/2010)


    and database_id < 2

    this was enough to answer the question...:-)

    Yes, if 10 would have been a possible answer it would have forced me to at least look at the rest of the Query.

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

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