UNION vs UNION ALL  discussion | Two approaches to solve the problem | Description
Before moving on to SQL queries let\'s dicuss what union and union all does\n UNION does a union without duplicates\n UNION ALL takes into account duplicates as
Before moving on to SQL queries let\'s dicuss what union and union all does\n* `UNION` does a union without duplicates\n* `UNION ALL` takes into account duplicates as well.\n\n**Approach1**: So basically if you do a union all of employeeid on two tables the count will be 1 for employee id which are only present in one table.\nOnce we do a union all we can do a count(empid) using groupby (empid) and then check if count of(empid) = 1 /*note that we need to use having with aggregate function here*/\n\n\n**Approach2** : Basically we firstly check from table 1 which entries are not present in table2 using not in clause and similarly we check from table 1 which entries are not present in table2 and then do a union of these two.\n\nPersonally I feel approach one is better because if you were to do it for 3 tables then you can  simply change your having condition.\n\n```\n\n/*SOLUTION 1*/\n\nSELECT T.employee_id from\n(\n    SELECT * FROM Employees \n    UNION ALL\n    SELECT * FROM Salaries\n) T\n\nGROUP BY T.EMPLOYEE_ID\nHAVING COUNT(T.EMPLOYEE_ID) = 1   /*can also use count(*) */\nORDER BY T.EMPLOYEE_ID ASC\n\n\n/*\nSOLUTION 2\n*/\n\nSELECT E.employee_id as employee_id from Employees E\nWHERE E.employee_id NOT IN \n(\n    SELECT employee_id from Salaries\n)\n\nUNION\n\nSELECT S.employee_id as employee_id from Salaries S\nWHERE S.employee_id NOT IN\n(\n    SELECT employee_id from Employees\n)\n\nORDER BY employee_id ASC\n\n```\n\nPS: Please upvote if you find this useful. It would motivate me to write more detailed solutions.
[]