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. Query the records that are not same in two different table.

Query the records that are not same in two different table.

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
6 Posts 4 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.
  • P Offline
    P Offline
    ptr_Electron
    wrote on last edited by
    #1

    Dear Experts, I am using MS SQL server 2005. I want to Query the records that are not same in two different table. ie., I have 2 different tables TABLE1, TABLE2 which are having same columns. (and most of the row are same) Q1 : I want to query the records of TABLE2 which are not avaliable in TABLE1 Q2 : I want to query the records of TABLE1 which are not avaliable in TABLE2 Needless to compare the primay key. Thanks in advance!!

    S B S 3 Replies Last reply
    0
    • P ptr_Electron

      Dear Experts, I am using MS SQL server 2005. I want to Query the records that are not same in two different table. ie., I have 2 different tables TABLE1, TABLE2 which are having same columns. (and most of the row are same) Q1 : I want to query the records of TABLE2 which are not avaliable in TABLE1 Q2 : I want to query the records of TABLE1 which are not avaliable in TABLE2 Needless to compare the primay key. Thanks in advance!!

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      SELECT a.*
      FROM table1 a left outer join table2 b
      --a.id, b.id are the columns that you would use to join the tables together
      ON a.id = b.id
      WHERE b.id IS NULL

      That will show you a list of records that are not in table2.

      Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

      P 1 Reply Last reply
      0
      • S Simon_Whale

        SELECT a.*
        FROM table1 a left outer join table2 b
        --a.id, b.id are the columns that you would use to join the tables together
        ON a.id = b.id
        WHERE b.id IS NULL

        That will show you a list of records that are not in table2.

        Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

        P Offline
        P Offline
        ptr_Electron
        wrote on last edited by
        #3

        Thanks a lot Simon_Whale But if I have one then one criatria to compare incase I also have a.id2 = b.id2..please suggest

        S 1 Reply Last reply
        0
        • P ptr_Electron

          Thanks a lot Simon_Whale But if I have one then one criatria to compare incase I also have a.id2 = b.id2..please suggest

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          the ON is what joins the two tables together is the second condition is more to restrict the results then you would put that in the WHERE clause.

          Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

          1 Reply Last reply
          0
          • P ptr_Electron

            Dear Experts, I am using MS SQL server 2005. I want to Query the records that are not same in two different table. ie., I have 2 different tables TABLE1, TABLE2 which are having same columns. (and most of the row are same) Q1 : I want to query the records of TABLE2 which are not avaliable in TABLE1 Q2 : I want to query the records of TABLE1 which are not avaliable in TABLE2 Needless to compare the primay key. Thanks in advance!!

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            Records of TABLE2 which are not avaliable in TABLE1

            select * from table2 where ColumnName not in (select ColumnName from table1 )

            TABLE1 which are not avaliable in TABLE2

            select * from table1 where ColumnName not in (select ColumnName from table2 )


            I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

            1 Reply Last reply
            0
            • P ptr_Electron

              Dear Experts, I am using MS SQL server 2005. I want to Query the records that are not same in two different table. ie., I have 2 different tables TABLE1, TABLE2 which are having same columns. (and most of the row are same) Q1 : I want to query the records of TABLE2 which are not avaliable in TABLE1 Q2 : I want to query the records of TABLE1 which are not avaliable in TABLE2 Needless to compare the primay key. Thanks in advance!!

              S Offline
              S Offline
              scottgp
              wrote on last edited by
              #6

              Also, check out EXCEPT http://weblogs.sqlteam.com/jeffs/archive/2007/05/02/sql-server-2005-using-except-and-intersect-to-compare-tables.aspx[^] Scott

              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