• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL Help please

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL Help please

    I have 2 tables, e.g. customers and orders. Foreign key between is customerID. I want to know which customers have zero orders.

    What SQL will achieve this?

    Thanks
    "Experience hath shewn, that even under the best forms of government those entrusted with power have, in time, and by slow operations, perverted it into tyranny. "


    Thomas Jefferson

    #2
    something like

    select c.field1, c.field2
    from customers c
    left join orders o
    on c.prikey = o.forkey
    where o.somefield is null


    off the top of me head like.

    Comment


      #3
      Have you not heard of EXISTS or IN ?

      select * from customers where customer_id not in (select customer_id from orders)

      BTW: you don't say what dbms you're using Oracle, SQL Server, Access, MySQL, etc...
      McCoy: "Medical men are trained in logic."
      Spock: "Trained? Judging from you, I would have guessed it was trial and error."

      Comment


        #4
        Using a subquery is a poor idea. That's what joins are for. You obviously have never worked on databases that get big.

        Comment


          #5
          Originally posted by DimPrawn View Post
          Using a subquery is a poor idea. That's what joins are for. You obviously have never worked on databases that get big.
          Are you sure? Seen the Execution Plan have we? Thought not.

          Comment


            #6
            Thanks dp, gives me what I need I think.
            "Experience hath shewn, that even under the best forms of government those entrusted with power have, in time, and by slow operations, perverted it into tyranny. "


            Thomas Jefferson

            Comment


              #7
              Originally posted by richard-af View Post
              Are you sure? Seen the Execution Plan have we? Thought not.

              Don't need to. 9 times out of 10 a join on two columns with appropriate indexes will generate a better execution plan than using IN, which ends up using a hash/match algorithm.

              Comment


                #8
                Originally posted by DimPrawn View Post
                Don't need to. 9 times out of 10 a join on two columns with appropriate indexes will generate a better execution plan than using IN, which ends up using a hash/match algorithm.
                agree. also avoids unneccessary table scans.

                Comment


                  #9
                  Originally posted by scotspine View Post
                  agree. also avoids unneccessary table scans.
                  9 times out of 10? Unnecessary table scans??!!!

                  I weep for you both. Amateurs.

                  Comment


                    #10
                    SP, if anyone else disagrees with me, can you please ban them?

                    Comment

                    Working...
                    X