返回介绍

solution / 1900-1999 / 1965.Employees With Missing Information / README_EN

发布于 2024-06-17 01:03:12 字数 3111 浏览 0 评论 0 收藏 0

1965. Employees With Missing Information

中文文档

Description

Table: Employees

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| employee_id | int   |
| name    | varchar |
+-------------+---------+
employee_id is the column with unique values for this table.
Each row of this table indicates the name of the employee whose ID is employee_id.

 

Table: Salaries

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| employee_id | int   |
| salary    | int   |
+-------------+---------+
employee_id is the column with unique values for this table.
Each row of this table indicates the salary of the employee whose ID is employee_id.

 

Write a solution 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 result format is in the following example.

 

Example 1:

Input: 
Employees table:
+-------------+----------+
| employee_id | name   |
+-------------+----------+
| 2       | Crew   |
| 4       | Haven  |
| 5       | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5       | 76071  |
| 1       | 22517  |
| 4       | 63539  |
+-------------+--------+
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.

Solutions

Solution 1: Subquery + Union

We can first find all employee_id that are not in the Salaries table from the Employees table, and then find all employee_id that are not in the Employees table from the Salaries table. Finally, we can combine the two results using the UNION operator, and sort the result by employee_id.

# Write your MySQL query statement below
SELECT employee_id
FROM Employees
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
UNION
SELECT employee_id
FROM Salaries
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文