azarasi / LeetCode 1965. Employees With Missing Information

Created Thu, 07 Apr 2022 12:05:28 +0800 Modified Wed, 18 Sep 2024 14:00:22 +0000

Table: Employees

Column NameType
employee_idint
namevarchar

employee_id is the primary key for this table. Each row of this table indicates the name of the employee whose ID is employee_id.

Table: Salaries

Column NameType
employee_idint
salaryint

employee_id is the primary key for this table. Each row of this table indicates the salary of the employee whose ID is employee_id.

Write an SQL query to report the IDs of all the employees with missing information. The information of an employee is missing if:

  • The employee’s name is missing, or
  • The employee’s salary is missing.

Return the result table ordered by employee_id in ascending order.

The query result format is in the following example.

Example 1:

Input: Employees table:

employee_idname
2Crew
4Haven
5Kristian

Salaries table:

employee_idsalary
576071
122517
463539

Output:

employee_id
1
2

Explanation: Employees 1, 2, 4, and 5 are working at this company. The name of employee 1 is missing. The salary of employee 2 is missing.

select employee_id
from (
         select employee_id
         from Employees
         union all
         select employee_id
         from Salaries
     ) as ans
group by employee_id
having count(employee_id) = 1
order by employee_id;