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. Database & SysAdmin
  3. Database
  4. SQL LEFT JOIN query

SQL LEFT JOIN query

Scheduled Pinned Locked Moved Database
databasesalesregex
7 Posts 3 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.
  • J Offline
    J Offline
    Joe Stansfield
    wrote on last edited by
    #1

    Hey all, I am still fairly knew to SQL query writing, and have searched to try and solve this by myself, however have had no luck! I am trying to write a query that returns information from SaleInformation and File_Info, with SaleInformation including all records that match a query and File_Info returning the matching File_Name(I have a relationship between them with PID the primary key in File_Info) if the PID is a match. However, my query is returning a result in every File_Name row return, even though I only have 3 entries in the File_Info PID column that match only 3 records in the Sale_information table. A gentle push in the right direction would be greatly appreciated! Oh, on a side note - If I change the query to "ON File_Info.PID = SaleInformation.PID" I get the 3 records that match returned. Here is the query I have at the moment: USE [C:\DATABASE\SALES.MDF] GO SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price FROM File_Info LEFT OUTER JOIN SaleInformation ON SaleInformation.Sale_price > 150000 It returns 183 rows. There are only 94 rows in the SaleInformation table and 3 in the File_Info table. I am very confused! Regards, Joe

    P M 2 Replies Last reply
    0
    • J Joe Stansfield

      Hey all, I am still fairly knew to SQL query writing, and have searched to try and solve this by myself, however have had no luck! I am trying to write a query that returns information from SaleInformation and File_Info, with SaleInformation including all records that match a query and File_Info returning the matching File_Name(I have a relationship between them with PID the primary key in File_Info) if the PID is a match. However, my query is returning a result in every File_Name row return, even though I only have 3 entries in the File_Info PID column that match only 3 records in the Sale_information table. A gentle push in the right direction would be greatly appreciated! Oh, on a side note - If I change the query to "ON File_Info.PID = SaleInformation.PID" I get the 3 records that match returned. Here is the query I have at the moment: USE [C:\DATABASE\SALES.MDF] GO SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price FROM File_Info LEFT OUTER JOIN SaleInformation ON SaleInformation.Sale_price > 150000 It returns 183 rows. There are only 94 rows in the SaleInformation table and 3 in the File_Info table. I am very confused! Regards, Joe

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

      Joe Stansfield wrote:

      I have a relationship between them with PID the primary key in File_Info

      Perhaps you think that that will somehow automatically affect the join? It doesn't; you need to specify the relationship in the join.

      J 1 Reply Last reply
      0
      • P PIEBALDconsult

        Joe Stansfield wrote:

        I have a relationship between them with PID the primary key in File_Info

        Perhaps you think that that will somehow automatically affect the join? It doesn't; you need to specify the relationship in the join.

        J Offline
        J Offline
        Joe Stansfield
        wrote on last edited by
        #3

        Ahh thanks! That is exactly what I thought would happen... Back to the old drawing board!

        1 Reply Last reply
        0
        • J Joe Stansfield

          Hey all, I am still fairly knew to SQL query writing, and have searched to try and solve this by myself, however have had no luck! I am trying to write a query that returns information from SaleInformation and File_Info, with SaleInformation including all records that match a query and File_Info returning the matching File_Name(I have a relationship between them with PID the primary key in File_Info) if the PID is a match. However, my query is returning a result in every File_Name row return, even though I only have 3 entries in the File_Info PID column that match only 3 records in the Sale_information table. A gentle push in the right direction would be greatly appreciated! Oh, on a side note - If I change the query to "ON File_Info.PID = SaleInformation.PID" I get the 3 records that match returned. Here is the query I have at the moment: USE [C:\DATABASE\SALES.MDF] GO SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price FROM File_Info LEFT OUTER JOIN SaleInformation ON SaleInformation.Sale_price > 150000 It returns 183 rows. There are only 94 rows in the SaleInformation table and 3 in the File_Info table. I am very confused! Regards, Joe

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          As Piebald suggest, you need the relationship in the join. You can include the filter as well but the relationship must be there.

          SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price
          FROM File_Info LEFT OUTER JOIN SaleInformation ON File_Info.PID = SaleInformation.PID
          and SaleInformation.Sale_price > 150000

          or

          SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price
          FROM File_Info LEFT OUTER JOIN SaleInformation ON File_Info.PID = SaleInformation.PID
          WHERE SaleInformation.Sale_price > 150000

          Never underestimate the power of human stupidity RAH

          J 1 Reply Last reply
          0
          • M Mycroft Holmes

            As Piebald suggest, you need the relationship in the join. You can include the filter as well but the relationship must be there.

            SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price
            FROM File_Info LEFT OUTER JOIN SaleInformation ON File_Info.PID = SaleInformation.PID
            and SaleInformation.Sale_price > 150000

            or

            SELECT File_Info.File_Name, SaleInformation.PID, SaleInformation.Sale_date, SaleInformation.Sale_price
            FROM File_Info LEFT OUTER JOIN SaleInformation ON File_Info.PID = SaleInformation.PID
            WHERE SaleInformation.Sale_price > 150000

            Never underestimate the power of human stupidity RAH

            J Offline
            J Offline
            Joe Stansfield
            wrote on last edited by
            #5

            Thanks! I had some trouble making it work - but it all came down to simply having the tables on the wrong sides of the LEFT OUTER JOIN. Switched them around and it is return as I wanted! Cheers for the assistance.

            M 1 Reply Last reply
            0
            • J Joe Stansfield

              Thanks! I had some trouble making it work - but it all came down to simply having the tables on the wrong sides of the LEFT OUTER JOIN. Switched them around and it is return as I wanted! Cheers for the assistance.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              This is one of the most useful articles [^]about joins I have seen

              Never underestimate the power of human stupidity RAH

              J 1 Reply Last reply
              0
              • M Mycroft Holmes

                This is one of the most useful articles [^]about joins I have seen

                Never underestimate the power of human stupidity RAH

                J Offline
                J Offline
                Joe Stansfield
                wrote on last edited by
                #7

                Thanks again! Great article - definitely helped me understand.

                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