🤔 문제 분석
1) Write an SQL query to report the first name, last name, city, and state of each person in the Person table.
2) If the address of a personId is not present in the Address table, report null instead.
3) Return the result table in any order.
💡 풀이
SELECT P.firstName, P.lastName, A.city, A.state
FROM Person AS P
LEFT JOIN Address AS A
ON P.personID = A.personID
+ 처음에는 null이라는 글자를 작성해야하는 줄 알고 아래와 같이 코드를 짰었다.
SELECT P.firstName, P.lastName
CASE
WHEN A.addressID IS NULL THEN A.state
ELSE A.city
END AS city,
CASE
WHEN A.state IS NULL THEN A.state
ELSE A.state
END AS stae
FROM Person AS P
LEFT JOIN Address AS A
ON P.personID = A.personID
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 17. Article Views I (1148) (0) | 2022.12.18 |
---|---|
[리트코드/SQL] 16. Customer Who Visited but Did Not Make Any Transactions (1581) (0) | 2022.12.18 |
[리트코드/SQL] 14. Second Highest Salary (176) (0) | 2022.12.16 |
[리트코드/SQL] 13. Tree node (608) (0) | 2022.12.16 |
[리트코드/SQL] 12. Rearrange Products Table (1795) (0) | 2022.12.16 |
댓글