Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Job Application Test from Hell

Job Application Test from Hell

Scheduled Pinned Locked Moved The Lounge
databasequestioncareer
64 Posts 33 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Brady Kelly

    I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

    Region

    Contact

    Cape Town

    Fred

    CapeTown

    Joe

    Cape Town

    Anna

    Durban

    John

    Durban

    Mary

    Johannesburg

    Frank

    Fig. 2

    Region

    Contact

    Durban

    John

    Durban

    Mary

    Johannesburg

    Frank

    Cape Town

    Anna

    CapeTown

    Fred

    Cape Town

    Joe

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #27

    Well, one way to do this would be to do something along the lines of:

    SELECT Region, Contact FROM WhatACrappyTest
    ORDER BY SUBSTRING(Region,2,1) DESC, Contact ASC

    This works based on the fact that the second character is ordered descending, and the contact orders ascending. This even takes the fact that your have CapeTown and Cape Town in the Region column. Obviously, the interviewer should be challenged on the validity of this question.

    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

    Forgive your enemies - it messes with their heads

    My blog | My articles | MoXAML PowerToys | Onyx

    C A P 3 Replies Last reply
    0
    • P PIEBALDconsult

      The correct answer is: "With which database system?" P.S. With Access/Jet via ADO.net:

      select * from RegionContact order by len(Region) mod 2,Region,Contact

      Region Contact


      Durban John
      Durban Mary
      Johannesburg Frank
      Cape Town Anna
      Cape Town Fred
      Cape Town Joe

      6 records affected.

      modified on Wednesday, November 24, 2010 4:03 PM

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #28

      PIEBALDconsult wrote:

      Cape Town

      According to the OP, one of those should be without a space.

      [Forum Guidelines]

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        Well, one way to do this would be to do something along the lines of:

        SELECT Region, Contact FROM WhatACrappyTest
        ORDER BY SUBSTRING(Region,2,1) DESC, Contact ASC

        This works based on the fact that the second character is ordered descending, and the contact orders ascending. This even takes the fact that your have CapeTown and Cape Town in the Region column. Obviously, the interviewer should be challenged on the validity of this question.

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #29

        If I was the interviewer, I'd have hired you based on that answer.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        1 Reply Last reply
        0
        • P Pete OHanlon

          Well, one way to do this would be to do something along the lines of:

          SELECT Region, Contact FROM WhatACrappyTest
          ORDER BY SUBSTRING(Region,2,1) DESC, Contact ASC

          This works based on the fact that the second character is ordered descending, and the contact orders ascending. This even takes the fact that your have CapeTown and Cape Town in the Region column. Obviously, the interviewer should be challenged on the validity of this question.

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #30

          Vader (Star Wars Gangsta Rap):

          Impressive, now release your anger; can't you sense that your friends are in danger?

          The impressiveness of your solution and you being who you are reminded me of that quote. :)

          [Forum Guidelines]

          1 Reply Last reply
          0
          • C Christian Graus

            Yeah, I expect that's the main thing he was to glean from the question.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #31

            Sure, but which one? Fred or Joe? I performed some data cleanup, so sue me. :-D Edit: WTF?! How'd it wind up attached to the wrong post? Sorry, just playing through...

            modified on Wednesday, November 24, 2010 5:44 PM

            1 Reply Last reply
            0
            • A AspDotNetDev

              PIEBALDconsult wrote:

              Cape Town

              According to the OP, one of those should be without a space.

              [Forum Guidelines]

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #32

              Sure, but which one? Fred or Joe? I performed some data cleanup, so sue me. :-D (Now lets see if it stays where it's supposed to be.) :mad:

              A 1 Reply Last reply
              0
              • P Pete OHanlon

                Well, one way to do this would be to do something along the lines of:

                SELECT Region, Contact FROM WhatACrappyTest
                ORDER BY SUBSTRING(Region,2,1) DESC, Contact ASC

                This works based on the fact that the second character is ordered descending, and the contact orders ascending. This even takes the fact that your have CapeTown and Cape Town in the Region column. Obviously, the interviewer should be challenged on the validity of this question.

                I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #33

                Pete O'Hanlon wrote:

                even takes the fact that your have CapeTown and Cape Town

                Have you tested that?

                A 2 Replies Last reply
                0
                • P PIEBALDconsult

                  Pete O'Hanlon wrote:

                  even takes the fact that your have CapeTown and Cape Town

                  Have you tested that?

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #34

                  He's only looking at the second character. In the case of "CapeTown", that'd be "a". In the case of "Cape Town", that'd be "a". What's there to test?

                  [Forum Guidelines]

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Sure, but which one? Fred or Joe? I performed some data cleanup, so sue me. :-D (Now lets see if it stays where it's supposed to be.) :mad:

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #35

                    PIEBALDconsult wrote:

                    Sure, but which one? Fred or Joe?

                    I hadn't noticed that.

                    [Forum Guidelines]

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Pete O'Hanlon wrote:

                      even takes the fact that your have CapeTown and Cape Town

                      Have you tested that?

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #36

                      With your above observation that "CapeTown" is first attached to Joe and then to Fred, your message makes more sense. I think I like this interview question... all kinds of details to help root out those who don't pay very good attention (I am apparently one of them). :)

                      [Forum Guidelines]

                      1 Reply Last reply
                      0
                      • C Chris Meech

                        The only obvious ordering sequence is the number of capitalized letters in Region, followed by the Region, followed by the Contact. :)

                        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #37

                        And the number of characters in an entry.

                        1 Reply Last reply
                        0
                        • B Brady Kelly

                          I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

                          Region

                          Contact

                          Cape Town

                          Fred

                          CapeTown

                          Joe

                          Cape Town

                          Anna

                          Durban

                          John

                          Durban

                          Mary

                          Johannesburg

                          Frank

                          Fig. 2

                          Region

                          Contact

                          Durban

                          John

                          Durban

                          Mary

                          Johannesburg

                          Frank

                          Cape Town

                          Anna

                          CapeTown

                          Fred

                          Cape Town

                          Joe

                          N Offline
                          N Offline
                          NickHighIQ
                          wrote on last edited by
                          #38

                          First thing's first, the order. It appears to be grouped by Region, the groups are ordered by MIN(Contact) (assuming MIN/MAX works in the way I expect, i.e. SELECT MAX('a', 'z') would return 'z' - never had to do an aggregate over varchar fields, thank GOD) and then ordered by Contact ascending. Thought process: Durban, JBurg, Cape Town - no obvious ordering there, but they're grouped... John, Mary - alphabetical Anna, Fred, Joe - alphabetical John, Frank, Anna - alphabetical (desc), so the earliest name in the alphabet in each region is used to order the regions... So, here's the SQL (SQL Server 2008):

                          SELECT
                          Region, Contact
                          FROM
                          TheStupidestTableEver
                          ORDER BY
                          MIN(Contact) OVER(PARTITION BY Region) desc, Contact

                          Results:

                          Region Contact

                          Durban John
                          Durban Mary
                          Johannesburg Frank
                          Cape Town Anna
                          Cape Town Fred
                          Cape Town Joe

                          So, do I win a prize? ;P In fact, I don't need one, that was a satisfying problem to solve :-D

                          1 Reply Last reply
                          0
                          • B Brady Kelly

                            The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. :) Here is mine, a little more general but very much the same:

                            select first.*, 0 outerSeq from (select top(select COUNT(*) from Contacts) * from Contacts where Region >= 'Durban' order by Region, Contact) first
                            union all
                            select second.*, 1 outerSeq from (select top (select COUNT(*) from Contacts) * from Contacts where Region < 'Durban' order by Region, Contact) as second
                            order by outerSeq

                            S Offline
                            S Offline
                            Serguei
                            wrote on last edited by
                            #39

                            SELECT Region, Contact
                            FROM Fig1
                            ORDER BY
                            CASE Region WHEN 'Durban' THEN 1 ELSE 2 END,
                            Contact

                            Tip, you can do:

                            SELECT TOP 100 PERCENT * FROM Fig1

                            Even better tip: Don't do that - your query doesn't guarantee (although will more than likely result in) the correct ordering. You should have sorted by outerSeq, Contact in the outer query. See http://blogs.msdn.com/b/queryoptteam/archive/2006/03/24/560396.aspx

                            B 1 Reply Last reply
                            0
                            • S Slacker007

                              SELECT * FROM YourTable ORDERBY Durban, Johannesburg, CapeTown INTHATORDER

                              M Offline
                              M Offline
                              Michael Kingsford Gray
                              wrote on last edited by
                              #40

                              Won't work. The middle "Cape Town" has the blank missing, and is "CapeTown" This makes it a very hard problem.

                              1 Reply Last reply
                              0
                              • S Serguei

                                SELECT Region, Contact
                                FROM Fig1
                                ORDER BY
                                CASE Region WHEN 'Durban' THEN 1 ELSE 2 END,
                                Contact

                                Tip, you can do:

                                SELECT TOP 100 PERCENT * FROM Fig1

                                Even better tip: Don't do that - your query doesn't guarantee (although will more than likely result in) the correct ordering. You should have sorted by outerSeq, Contact in the outer query. See http://blogs.msdn.com/b/queryoptteam/archive/2006/03/24/560396.aspx

                                B Offline
                                B Offline
                                Brady Kelly
                                wrote on last edited by
                                #41

                                Thanks, I especially like the top 100 percent

                                1 Reply Last reply
                                0
                                • B Brady Kelly

                                  I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

                                  Region

                                  Contact

                                  Cape Town

                                  Fred

                                  CapeTown

                                  Joe

                                  Cape Town

                                  Anna

                                  Durban

                                  John

                                  Durban

                                  Mary

                                  Johannesburg

                                  Frank

                                  Fig. 2

                                  Region

                                  Contact

                                  Durban

                                  John

                                  Durban

                                  Mary

                                  Johannesburg

                                  Frank

                                  Cape Town

                                  Anna

                                  CapeTown

                                  Fred

                                  Cape Town

                                  Joe

                                  A Offline
                                  A Offline
                                  altncsab
                                  wrote on last edited by
                                  #42

                                  This is my solution. Because there was no pre-condition then anything is allowed :-D

                                  declare @T_MyTable Table(Region varchar(200), Contact varchar(200))

                                  select case when Contact = 'Fred' and Region like 'Cape%Town' then 'CapeTown'
                                  when Region like 'Cape%Town' then 'Cape Town'
                                  else Region end Region,
                                  Contact
                                  from @T_MyTable
                                  order by substring(Region,2,1) desc, Contact

                                  1 Reply Last reply
                                  0
                                  • B Brady Kelly

                                    I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

                                    Region

                                    Contact

                                    Cape Town

                                    Fred

                                    CapeTown

                                    Joe

                                    Cape Town

                                    Anna

                                    Durban

                                    John

                                    Durban

                                    Mary

                                    Johannesburg

                                    Frank

                                    Fig. 2

                                    Region

                                    Contact

                                    Durban

                                    John

                                    Durban

                                    Mary

                                    Johannesburg

                                    Frank

                                    Cape Town

                                    Anna

                                    CapeTown

                                    Fred

                                    Cape Town

                                    Joe

                                    S Offline
                                    S Offline
                                    Simon_Duckett
                                    wrote on last edited by
                                    #43

                                    SELECT [Region], [Contact] FROM Table1 ORDER BY SUBSTRING(REVERSE([Region]), 2, 1), [Contact]

                                    J 1 Reply Last reply
                                    0
                                    • B Brady Kelly

                                      I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

                                      Region

                                      Contact

                                      Cape Town

                                      Fred

                                      CapeTown

                                      Joe

                                      Cape Town

                                      Anna

                                      Durban

                                      John

                                      Durban

                                      Mary

                                      Johannesburg

                                      Frank

                                      Fig. 2

                                      Region

                                      Contact

                                      Durban

                                      John

                                      Durban

                                      Mary

                                      Johannesburg

                                      Frank

                                      Cape Town

                                      Anna

                                      CapeTown

                                      Fred

                                      Cape Town

                                      Joe

                                      K Offline
                                      K Offline
                                      KP Lee
                                      wrote on last edited by
                                      #44

                                      This doesn't look like a question geared to testing your SQL skills, but your skill at defining "real" requirements from something that seems very silly at first. 1. Assume the person asking this, isn't a lune escaped from the asylum. 2. Try to find the underlying order that they are requesting. 3. ASK them what the underlying order is, suggesting a possibility. 4 ASK them the schema information you need in order to write the query. Sort of like: I see these regions are all in South Africa. I'm not that informed about that area, are you ordering this query by population? No? What is the order criteria you are using? Then find out if the criteria is in the table you are querying? Or they could be testing your knowledge of DB design and waiting for you to ask why the H the region and the name are stored in the same table in the first place. Failing all that, just answer the question. There are a bunch of ways to do it. You can throw in a case statement in a batch select and select the two fields and order by the case result, the union all solution would work, creating a temp table and joining with it is a third option.

                                      1 Reply Last reply
                                      0
                                      • B Brady Kelly

                                        I'm not looking for an answer here, I found my own, but this is quite a hard question. Given the table from Fig.1, write an SQL Select statement that would re-organize the results to look like Fig.2 Fig. 1

                                        Region

                                        Contact

                                        Cape Town

                                        Fred

                                        CapeTown

                                        Joe

                                        Cape Town

                                        Anna

                                        Durban

                                        John

                                        Durban

                                        Mary

                                        Johannesburg

                                        Frank

                                        Fig. 2

                                        Region

                                        Contact

                                        Durban

                                        John

                                        Durban

                                        Mary

                                        Johannesburg

                                        Frank

                                        Cape Town

                                        Anna

                                        CapeTown

                                        Fred

                                        Cape Town

                                        Joe

                                        J Offline
                                        J Offline
                                        James H
                                        wrote on last edited by
                                        #45

                                        SELECT Region, Contact FROM RegionTable ORDER BY SubString(Region,2,1) DESC, Contact ASC

                                        1 Reply Last reply
                                        0
                                        • S Simon_Duckett

                                          SELECT [Region], [Contact] FROM Table1 ORDER BY SUBSTRING(REVERSE([Region]), 2, 1), [Contact]

                                          J Offline
                                          J Offline
                                          James H
                                          wrote on last edited by
                                          #46

                                          Ha - you beat me

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups