LeetCode SQL Problem🧑💻 Day: 1
Problem number: 1965. Employees With Missing Information
Hey folks! I’m starting a series of articles where I’ll be posting interesting SQL problems on a daily basis. I hope you all will love it! I will provide detailed solutions to each problem, so please feel free to share your thoughts and solutions in the comments. I’m looking forward to hearing from you!
Problem Statement:
Table: Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
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 Name | Type |
+-------------+---------+
| employee_id | int |
| salary | int |
+-------------+---------+
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.
Get Ravi Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
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.Details Discussion and Solution
- Here we are provided with two tables one is Employee and the other is Salaries
2. Perform a left join of the Employee table with the Salaries table to retrieve all possible combinations from the left table
select e.employee_id as emp_1,e.name as name, s.salary as salary
from Employees e
left join Salaries s
on e.employee_id = s.employee_id3. Perform a right join of the Employee table with the Salaries table to retrieve all possible combinations from the left table
select s.employee_id as emp_2,e.name as name, s.salary as salary
from Employees e
right join Salaries s
on e.employee_id = s.employee_id4. Do a union of both Step 2 and Step 3 to get all the final combination
with cte1 as(
select e.employee_id as emp_1,e.name as name, s.salary as salary
from Employees e
left join Salaries s
on e.employee_id = s.employee_id
union
select s.employee_id as emp_2,e.name as name, s.salary as salary
from Employees e
right join Salaries s
on e.employee_id = s.employee_id)5. Select the required column from step 4 where the Salary is NULL or Name is NULL
with cte1 as(
select e.employee_id as emp_1,e.name as name, s.salary as salary
from Employees e
left join Salaries s
on e.employee_id = s.employee_id
union
select s.employee_id as emp_2,e.name as name, s.salary as salary
from Employees e
right join Salaries s
on e.employee_id = s.employee_id)
select emp_1 as employee_id from cte1
where name is null or salary is null
order by emp_1I am a Data Science enthusiast🌺, Learning and exploring how Math, Business, and Technology can help us to make better decisions in the field of data science.
Want to read more: https://medium.com/@ravikumar10593/
Find my all handles: https://linktr.ee/ravikumar10593
If this article helped you, don’t forget to Follow, like, and share it with your friends👍Happy Learning!!
SQL Top Post:









