Leetcode SQL 50 -- 1378. Replace Employee ID With The Unique Identifier

  1. Question
    1. Table name: Employees
    2. Table name: EmployeeUNI
  2. Explaination
  3. Solution

Question

Table name: Employees

Column Name Type
id int
name varchar

id is the primary key (column with unique values) for this table.
Each row of this table contains the id and the name of an employee in a company.

Table name: EmployeeUNI

Column Name Type
id int
unique_id int

(id, unique_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the company.

Write a solution to show the unique ID of each user,
If a user does not have a unique ID replace just show null.
Return the result table in any order.

Explaination

In this question, we need to use JOIN:

FROM <table1>
JOIN <table2> ON <table1.column_n> = <table2.column_m>

JOIN always comes with ON, which indicates the column used as a reference for joining.
Also, there are different types referring to different usage.

We are trying to include unique_id to the table Employees
Since we need to include every users even if they don’t have a unique ID, we need to use LEFT JOIN.

Solution

SELECT EmployeeUNI.unique_id, Employees.name 
FROM Employees 
LEFT JOIN EmployeeUNI ON EmployeeUNI.id = Employees.id;

More SQL 50 questions: here


Please cite the source for reprints, feel free to verify the sources cited in the article, and point out any errors or lack of clarity of expression. You can comment in the comments section below or email to GreenMeeple@yahoo.com
This Repo