https://leetcode.com/problems/combine-two-tables/
Question:
Write an SQL query to report the first name,
last name, city, and state of each person in the Person table.
If the address of a personId is not
present in the Address table, report null instead.
|
cs |
Solution:
There are two table, Person and Address. So we can see that we have to use two table at the same time.
In this case, We need 'JOIN' to solve this question. Because question requires the return of null
SELECT P.firstName , P.lastName, A.city, A.state
FROM Person AS P
LEFT JOIN Address AS A ON P.personId = A.personId |
cs |
'Leetcode' 카테고리의 다른 글
leetcode Reverse Integer (0) | 2022.05.29 |
---|---|
Add Two Numbers leetcode 2 (0) | 2022.05.29 |
[MySQL] 176. Second Highest Salary[LeetCode Medium] - 1 (0) | 2022.05.25 |
[MySQL] 3. 175. Combine Two Tables [LeetCode Easy] (0) | 2022.05.25 |
[LeetCode Medium] 3. Longest Substring Without Repeating Characters [C/C++] (0) | 2022.05.25 |