Leetcode

[MySQL] 175. Combine Two Tables [LeetCode Easy]

치킨먹고싶어요 2022. 5. 25. 22:27

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

 

 

 

combine