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. Other Discussions
  3. The Weird and The Wonderful
  4. SQL query - which version do you prefer? [modified]

SQL query - which version do you prefer? [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasec++questionannouncement
10 Posts 8 Posters 3 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.
  • G Offline
    G Offline
    GuyThiebaut
    wrote on last edited by
    #1

    These two queries return the same result set(different results as different fields). A colleague wrote the first query and I wrote the second. Now I think the first one looks just plain ugly, however the execution plan is not too dissimilar to the second query, which I wrote. Is there any rationality to my response of Urghhhh? My colleague's version:

    select 
    sat.salesnumber,
    pjq.pickjobnumber
     
    from
     
    (select salesnumber, rownumber, invoiceaccount
    from salestable
    where dataset = 'wtl'
    and ltrim(salesnumber) = '331299') as sat
     
    join
     
    (select * from 
    pickjobtrans) as pjt
     
    on sat.rownumber = pjt.picksalesrecid
     
    join
    (select * from
    pickjobqueue) as pjq
     
    on pjt.pickjobnumber = pjq.pickjobnumber
    group by sat.salesnumber, pjq.pickjobnumber
    

    My version:

    select distinct pjq.accountnumber,pjq.status
    from pickjobqueue pjq
    right join pickjobtrans pjt
    on pjq.pickjobnumber = pjt.pickjobnumber
    right join salestable sal
    on pjt.picksalesrecid = sal.rownumber
    where ltrim(sal.salesnumber) = '331299'
    
    You always pass failure on the way to success.
    G S S 3 Replies Last reply
    0
    • G GuyThiebaut

      These two queries return the same result set(different results as different fields). A colleague wrote the first query and I wrote the second. Now I think the first one looks just plain ugly, however the execution plan is not too dissimilar to the second query, which I wrote. Is there any rationality to my response of Urghhhh? My colleague's version:

      select 
      sat.salesnumber,
      pjq.pickjobnumber
       
      from
       
      (select salesnumber, rownumber, invoiceaccount
      from salestable
      where dataset = 'wtl'
      and ltrim(salesnumber) = '331299') as sat
       
      join
       
      (select * from 
      pickjobtrans) as pjt
       
      on sat.rownumber = pjt.picksalesrecid
       
      join
      (select * from
      pickjobqueue) as pjq
       
      on pjt.pickjobnumber = pjq.pickjobnumber
      group by sat.salesnumber, pjq.pickjobnumber
      

      My version:

      select distinct pjq.accountnumber,pjq.status
      from pickjobqueue pjq
      right join pickjobtrans pjt
      on pjq.pickjobnumber = pjt.pickjobnumber
      right join salestable sal
      on pjt.picksalesrecid = sal.rownumber
      where ltrim(sal.salesnumber) = '331299'
      
      You always pass failure on the way to success.
      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      The one that runs faster :)

      #region signature my articles #endregion

      1 Reply Last reply
      0
      • G GuyThiebaut

        These two queries return the same result set(different results as different fields). A colleague wrote the first query and I wrote the second. Now I think the first one looks just plain ugly, however the execution plan is not too dissimilar to the second query, which I wrote. Is there any rationality to my response of Urghhhh? My colleague's version:

        select 
        sat.salesnumber,
        pjq.pickjobnumber
         
        from
         
        (select salesnumber, rownumber, invoiceaccount
        from salestable
        where dataset = 'wtl'
        and ltrim(salesnumber) = '331299') as sat
         
        join
         
        (select * from 
        pickjobtrans) as pjt
         
        on sat.rownumber = pjt.picksalesrecid
         
        join
        (select * from
        pickjobqueue) as pjq
         
        on pjt.pickjobnumber = pjq.pickjobnumber
        group by sat.salesnumber, pjq.pickjobnumber
        

        My version:

        select distinct pjq.accountnumber,pjq.status
        from pickjobqueue pjq
        right join pickjobtrans pjt
        on pjq.pickjobnumber = pjt.pickjobnumber
        right join salestable sal
        on pjt.picksalesrecid = sal.rownumber
        where ltrim(sal.salesnumber) = '331299'
        
        You always pass failure on the way to success.
        S Offline
        S Offline
        s hatchard
        wrote on last edited by
        #3

        I think having a column called salesnumber which is actually a char datatype is a bit of a db design coding horror too :)

        G 1 Reply Last reply
        0
        • G GuyThiebaut

          These two queries return the same result set(different results as different fields). A colleague wrote the first query and I wrote the second. Now I think the first one looks just plain ugly, however the execution plan is not too dissimilar to the second query, which I wrote. Is there any rationality to my response of Urghhhh? My colleague's version:

          select 
          sat.salesnumber,
          pjq.pickjobnumber
           
          from
           
          (select salesnumber, rownumber, invoiceaccount
          from salestable
          where dataset = 'wtl'
          and ltrim(salesnumber) = '331299') as sat
           
          join
           
          (select * from 
          pickjobtrans) as pjt
           
          on sat.rownumber = pjt.picksalesrecid
           
          join
          (select * from
          pickjobqueue) as pjq
           
          on pjt.pickjobnumber = pjq.pickjobnumber
          group by sat.salesnumber, pjq.pickjobnumber
          

          My version:

          select distinct pjq.accountnumber,pjq.status
          from pickjobqueue pjq
          right join pickjobtrans pjt
          on pjq.pickjobnumber = pjt.pickjobnumber
          right join salestable sal
          on pjt.picksalesrecid = sal.rownumber
          where ltrim(sal.salesnumber) = '331299'
          
          You always pass failure on the way to success.
          S Offline
          S Offline
          snorkie
          wrote on last edited by
          #4

          I am not a fan of SELECT *, It returns too much data, taking up resources along the way. That being said, my only other complaint is the lack of formatting, but that is just code style. I find the extra formatting makes it easier to find the part I want to work on. SELECT DISTINCT pjq.accountnumber, pjq.status FROM pickjobqueue pjq RIGHT JOIN pickjobtrans pjt ON (pjq.pickjobnumber = pjt.pickjobnumber) RIGHT JOIN salestable sal ON (pjt.picksalesrecid = sal.rownumber) WHERE LTRIM(sal.salesnumber) = '331299' Hogan

          G V 2 Replies Last reply
          0
          • S snorkie

            I am not a fan of SELECT *, It returns too much data, taking up resources along the way. That being said, my only other complaint is the lack of formatting, but that is just code style. I find the extra formatting makes it easier to find the part I want to work on. SELECT DISTINCT pjq.accountnumber, pjq.status FROM pickjobqueue pjq RIGHT JOIN pickjobtrans pjt ON (pjq.pickjobnumber = pjt.pickjobnumber) RIGHT JOIN salestable sal ON (pjt.picksalesrecid = sal.rownumber) WHERE LTRIM(sal.salesnumber) = '331299' Hogan

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #5

            I take your point on formatting.

            snorkie wrote:

            I am not a fan of SELECT *, It returns too much data, taking up resources along the way

            I am in the same camp as you here, however when I looked at the execution plan both queries have very similar execution plans. In the end neither was faster. My position is more that it just looks lazy using those select* statements. I also find a beauty in being able to write code that is short and concise. Maybe I am being pedantic and need to let go... -- modified at 15:28 Tuesday 6th November, 2007

            You always pass failure on the way to success.
            P 1 Reply Last reply
            0
            • S s hatchard

              I think having a column called salesnumber which is actually a char datatype is a bit of a db design coding horror too :)

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              Yes - it's a Mircosoft XAL database. It would make more sense space-wise as well as a varchar, in this case, will take more space than an int type which only takes 4 bytes. Microsoft bought the database from Navision who bought it from Daamgard before it was a SQL database. Let that be a warning to database design - get it wrong at the beginning and you can be left with problems for the life of the product.

              You always pass failure on the way to success.
              1 Reply Last reply
              0
              • G GuyThiebaut

                I take your point on formatting.

                snorkie wrote:

                I am not a fan of SELECT *, It returns too much data, taking up resources along the way

                I am in the same camp as you here, however when I looked at the execution plan both queries have very similar execution plans. In the end neither was faster. My position is more that it just looks lazy using those select* statements. I also find a beauty in being able to write code that is short and concise. Maybe I am being pedantic and need to let go... -- modified at 15:28 Tuesday 6th November, 2007

                You always pass failure on the way to success.
                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                GuyThiebaut wrote:

                I am in the same camp as you here, however when I looked at the execution plan both queries have very similar execution plans. In the end neither was faster.

                To a large extent, the execution plan is immaterial here. What matters more is the amount of data that is returned over the network. If you only need one column, why put more load on what may already be a busy network by returning 20?

                Deja View - the feeling that you've seen this post before.

                P 1 Reply Last reply
                0
                • P Pete OHanlon

                  GuyThiebaut wrote:

                  I am in the same camp as you here, however when I looked at the execution plan both queries have very similar execution plans. In the end neither was faster.

                  To a large extent, the execution plan is immaterial here. What matters more is the amount of data that is returned over the network. If you only need one column, why put more load on what may already be a busy network by returning 20?

                  Deja View - the feeling that you've seen this post before.

                  P Offline
                  P Offline
                  Phil Osborne
                  wrote on last edited by
                  #8

                  Select *....just say no. It is a lazy approach and as Pete points out you can end up creating a lot of network traffic for no benefit.

                  R 1 Reply Last reply
                  0
                  • S snorkie

                    I am not a fan of SELECT *, It returns too much data, taking up resources along the way. That being said, my only other complaint is the lack of formatting, but that is just code style. I find the extra formatting makes it easier to find the part I want to work on. SELECT DISTINCT pjq.accountnumber, pjq.status FROM pickjobqueue pjq RIGHT JOIN pickjobtrans pjt ON (pjq.pickjobnumber = pjt.pickjobnumber) RIGHT JOIN salestable sal ON (pjt.picksalesrecid = sal.rownumber) WHERE LTRIM(sal.salesnumber) = '331299' Hogan

                    V Offline
                    V Offline
                    Vasudevan Deepak Kumar
                    wrote on last edited by
                    #9

                    snorkie wrote:

                    I am not a fan of SELECT *,

                    I support you. Even in the case of selecting all the columns of the table (only remote combinations of applications), I prefer having them enumerated instead of *. I feel it is more readable and friendly too not to mention about the significant gains in terms of performance. For that matter, I think, even C# takes that stand. Like Java, where java.lang.* no longer works for namespace inclusions in managed code. Isn't it?

                    Vasudevan Deepak Kumar Personal Homepage Tech Gossips
                    Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

                    1 Reply Last reply
                    0
                    • P Phil Osborne

                      Select *....just say no. It is a lazy approach and as Pete points out you can end up creating a lot of network traffic for no benefit.

                      R Offline
                      R Offline
                      Russell Jones
                      wrote on last edited by
                      #10

                      not to mention the possibility of some adding a big field to the end of the table (blob for instance) and your code suddenly grinding to a halt. I won't use select * even if i want every field as you have no idea what changes may come later Russ

                      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