Fast and Simple Solution by using group by and count
Problem Statement\nGiven two tables Employees and Salaries, where Employees has columns employee_id and name, and Salaries has columns employee_id and salary, t
# Problem Statement\nGiven two tables `Employees` and `Salaries`, where `Employees` has columns `employee_id` and `name`, and `Salaries` has columns `employee_id` and `salary`, the task is to retrieve the `employee_id` of employees who appear in either `Employees` or `Salaries` table but not in both.\n\n## Intuition\nThe problem involves identifying unique `employee_id` values that are present in either `Employees` or `Salaries` but not in both.\n\n## Approach\n1. **Use of `UNION ALL`:**\n   - Utilize the `UNION ALL` operator to combine the rows from both tables (`Employees` and `Salaries`) into a single result set.\n   - The `UNION ALL` operator is used to include duplicates if any exist.\n\n2. **Grouping and Filtering:**\n   - Create a subquery (`t`) to represent the combined result set.\n   - Use the `GROUP BY` clause on `employee_id` to group the results based on unique employee identifiers.\n   - Use the `HAVING` clause to filter out rows where the count of occurrences for an `employee_id` is less than 2, indicating that the employee is present in either `Employees` or `Salaries` but not in both.\n\n3. **Ordering Results:**\n   - Use the `ORDER BY` clause to order the final result set based on `employee_id`.\n\n## Complexity\n- **Time complexity:**\n  - The time complexity is primarily determined by the use of `UNION ALL` and the grouping operation. It is generally efficient and can be considered as $$O(n)$$, where $$n$$ is the total number of rows in both tables.\n\n- **Space complexity:**\n  - The space complexity is also $$O(n)$$, where $$n$$ is the total number of rows in both tables. This accounts for the storage of intermediate results during the use of `UNION ALL` and the grouping operation.\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT employee_id\nFROM (\n    SELECT *\n    FROM Employees\n    UNION ALL \n    SELECT *\n    FROM Salaries\n) AS t\nGROUP BY employee_id\nHAVING COUNT(*) < 2\nORDER BY employee_id \n```\n![image.png](https://assets.leetcode.com/users/images/e08fef18-bc2a-4c3c-94b6-28421087c127_1703331361.4534283.png)\n
['MySQL']