Comparing Two Different Tables, Showing Values In Table 1 Not in Table 2

  • Hello,

    I'd like to compare two tables, and show values found in table 1 which are NOT in table 2, then same for table 2, show values in table 2 which are NOT in table 1.

    Here's what I came up with, looks ok?

    select T1.[Name], T2.[Address]

    from [dbo].[My Table 1] as T1

    left join [dbo].[My Table 2] as T2 on T1.[Name]=T2.[Name]

    where T2.[Address] is not null

    Thanks

  • SELECT

    TableSource = CASE WHEN T1.[Name] IS NULL THEN 'Table2' ELSE 'Table1' END,

    T1.[Name],

    T1.[Address],

    T2.[Name],

    T2.[Address]

    FROM [dbo].[My Table 1] T1

    FULL OUTER JOIN [dbo].[My Table 2] T2

    ON T1.[Name] = T2.[Name]

    WHERE NOT T1.[Name] = T2.[Name]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply