0

I have two datasets with similar data but I need to find the differences in Department that don't show in table 1 but in table 2. Here is an example of what I have:

table1 
ID      Department     Location 
 1        Sales          NY
 2        Tech           AL
 3        Sales          NC
 4        Consult        CA

table2
 ID     Department     Location
  1        Sales          NY
  2        Sales          WY
  3        Tech           NC 
  4        Consult        CA

what I want is to show the ones that don't equal to table 1:

 ID       Department    Location
  2        Sales           WY
  3        Tech            NC

I have 2 tables with 1000+ rows so I just want to see if this is the correct way or is there something I am missing.

So far I have tried:

 Select Department
 From table1
 minus 
 Select Department
 From table2
3
  • If you run it you will probably find that doesn't work, because you are only checking a single column. Sales and Tech are both in table 2 so result should be an empty set.
    – topsail
    Mar 17, 2023 at 17:33
  • Okay I see what you're saying how would I fix that then? Mar 17, 2023 at 17:37
  • Include the other field so you can check duplicates on department/location Select Department, Location From table1 minus Select Department, Location From table2
    – topsail
    Mar 17, 2023 at 18:47

2 Answers 2

0

You may use below query to get the results,

SELECT table2.ID, table2.Department, table2.[Location]
FROM table2
LEFT JOIN table1
ON table2.ID = table1.ID
WHERE table1.Department <> table2.Department

Thanks

0

Try this:

SELECT ID, Department, Location FROM table2
WHERE (ID, Department, Location) NOT IN(SELECT * FROM table1);

With this query you select all the rows in table2 that there aren't in the second table1. You can do this because they are similar tables (considering the number of columns and the types of columns).

2
  • Would you mind editing your answer and adding some explanations.
    – SelVazi
    Mar 20, 2023 at 22:20
  • Please read "How to Answer" and "Explaining entirely code-based answers". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. Mar 21, 2023 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.