|
| 1 | +-- Table: Employees |
| 2 | + |
| 3 | +-- +---------------+---------+ |
| 4 | +-- | Column Name | Type | |
| 5 | +-- +---------------+---------+ |
| 6 | +-- | id | int | |
| 7 | +-- | name | varchar | |
| 8 | +-- +---------------+---------+ |
| 9 | +-- id is the primary key (column with unique values) for this table. |
| 10 | +-- Each row of this table contains the id and the name of an employee in a company. |
| 11 | + |
| 12 | + |
| 13 | +-- Table: EmployeeUNI |
| 14 | + |
| 15 | +-- +---------------+---------+ |
| 16 | +-- | Column Name | Type | |
| 17 | +-- +---------------+---------+ |
| 18 | +-- | id | int | |
| 19 | +-- | unique_id | int | |
| 20 | +-- +---------------+---------+ |
| 21 | +-- (id, unique_id) is the primary key (combination of columns with unique values) for this table. |
| 22 | +-- Each row of this table contains the id and the corresponding unique id of an employee in the company. |
| 23 | + |
| 24 | + |
| 25 | +-- Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null. |
| 26 | + |
| 27 | +-- Return the result table in any order. |
| 28 | + |
| 29 | +-- The result format is in the following example. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +-- Example 1: |
| 34 | + |
| 35 | +-- Input: |
| 36 | +-- Employees table: |
| 37 | +-- +----+----------+ |
| 38 | +-- | id | name | |
| 39 | +-- +----+----------+ |
| 40 | +-- | 1 | Alice | |
| 41 | +-- | 7 | Bob | |
| 42 | +-- | 11 | Meir | |
| 43 | +-- | 90 | Winston | |
| 44 | +-- | 3 | Jonathan | |
| 45 | +-- +----+----------+ |
| 46 | +-- EmployeeUNI table: |
| 47 | +-- +----+-----------+ |
| 48 | +-- | id | unique_id | |
| 49 | +-- +----+-----------+ |
| 50 | +-- | 3 | 1 | |
| 51 | +-- | 11 | 2 | |
| 52 | +-- | 90 | 3 | |
| 53 | +-- +----+-----------+ |
| 54 | +-- Output: |
| 55 | +-- +-----------+----------+ |
| 56 | +-- | unique_id | name | |
| 57 | +-- +-----------+----------+ |
| 58 | +-- | null | Alice | |
| 59 | +-- | null | Bob | |
| 60 | +-- | 2 | Meir | |
| 61 | +-- | 3 | Winston | |
| 62 | +-- | 1 | Jonathan | |
| 63 | +-- +-----------+----------+ |
| 64 | +-- Explanation: |
| 65 | +-- Alice and Bob do not have a unique ID, We will show null instead. |
| 66 | +-- The unique ID of Meir is 2. |
| 67 | +-- The unique ID of Winston is 3. |
| 68 | +-- The unique ID of Jonathan is 1. |
| 69 | + |
| 70 | + |
| 71 | +-- Solution (PostgreSQL): |
| 72 | +select case |
| 73 | + when EmployeeUNI.unique_id is null then null |
| 74 | + else EmployeeUNI.unique_id |
| 75 | + end as unique_id, |
| 76 | + Employees.name |
| 77 | +from Employees |
| 78 | + left join EmployeeUNI on (Employees.id = EmployeeUNI.id); |
0 commit comments