Note that there are some explanatory texts on larger screens.

plurals
  1. POsql query to return differences between two tables
    primarykey
    data
    text
    <p>I am trying to compare two tables, SQL Server, to verify some data. I want to return all the rows from both tables where data is either in one or the other. In essence, I want to show all the discrepancies. I need to check three pieces of data in doing so, FirstName, LastName and Product.</p> <p>I'm fairly new to SQL and it seems like a lot of the solutions I'm finding are over complicating things. I don't have to worry about NULLs.</p> <p>I started by trying something like this:</p> <pre><code>SELECT DISTINCT [First Name], [Last Name], [Product Name] FROM [Temp Test Data] WHERE ([First Name] NOT IN (SELECT [First Name] FROM [Real Data])) </code></pre> <p>I'm having trouble taking this further though.</p> <p>Thanks!</p> <p>EDIT:</p> <p>Based on the answer by @treaschf I have been trying to use a variation of the following query:</p> <pre><code>SELECT td.[First Name], td.[Last Name], td.[Product Name] FROM [Temp Test Data] td FULL OUTER JOIN [Data] AS d ON td.[First Name] = d.[First Name] AND td.[Last Name] = d.[Last Name] WHERE (d.[First Name] = NULL) AND (d.[Last Name] = NULL) </code></pre> <p>But I keep getting 0 results back, when I know that there is at least 1 row in td that is not in d.</p> <p>EDIT:</p> <p>Ok, I think I figured it out. At least in my few minutes of testing it seems to work good enough.</p> <pre><code>SELECT [First Name], [Last Name] FROM [Temp Test Data] AS td WHERE (NOT EXISTS (SELECT [First Name], [Last Name] FROM [Data] AS d WHERE ([First Name] = td.[First Name]) OR ([Last Name] = td.[Last Name]))) </code></pre> <p>This is basically going to tell me what is in my test data that is <em>not</em> in my real data. Which is completely fine for what I need to do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload