관리 메뉴

물 만날 물고기

[LeetCode/MYSQL] - 175. Combine Two Tables 본문

DB & SQL/Leetcode

[LeetCode/MYSQL] - 175. Combine Two Tables

Lung Fish 2023. 6. 28. 11:39

🔍 예상 검색어

더보기

# 물만날물고기

# LeetCode

# SQL

# Combine Two Tables


해당 포스팅은 LeetCode/MYSQL의 Combine Two Tables 문제에 대한 풀이를 정리하였습니다.

 
 
▶ 문제

Combine Two Tables - LeetCode

 

Combine Two Tables - LeetCode

Can you solve this real interview question? Combine Two Tables - Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | personId | int | | lastName | varchar | | firstName | varchar | +-------------+---------+ personId i

leetcode.com

 
▶ 내 정답

# Write your MySQL query statement below

-- Person : personId, lastName, firstName   
-- Address: addressId,  personId, city, state                    

-- 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.


SELECT P.firstName , P.lastName, IFNULL(A.city,NULL) AS city , IFNULL(A.state,NULL) AS state
FROM Person AS P 
LEFT JOIN Address AS A ON P.personId = A.personId

▶ 다른 사람 정답 (1)

select p.firstName , p.lastName, a.city , a.state 
    from Person p LEFT JOIN Address a on p.personId  = a.personId

Approach 1: Using conventional method by making equal both the attributes of the table to join them
i.e. on p.personId = a.personId

select p.firstName , p.lastName, a.city , a.state 
    from Person p LEFT JOIN Address a USING(personId)

Approach 2: We can see that both the attributes of the table to compare have same name i.e. personId, so instead of writing it multiple time we will use USING() function
i.e. USING(personId)
 
 
▶ 다른 사람 정답 (2)
 
METHOD -> JOIN

SELECT p.firstName, p.lastName, a.city, a.state 
FROM Person p left join Address a
ON p.personId = a.personId;

METHOD2 -> UNION

SELECT p.firstName, p.lastName, a.city, a.state from Person p, Address a
WHERE p.personId = a.personId
UNION
SELECT  p.firstName, p.lastName, null, null from Person p
WHERE p.personId NOT IN (Select personId from Address);

 
▶ 총평
 
이 문제는 단순 JOIN 함수 사용 문제인데, "If the address of a personId is not present in the Address table, report null instead." 내용을 보고 IFNULL 처리 구문을 추가 하였는데, 일반적으로 값이 없으면 NULL이 출력되기 때문에 다른 사람들 정답에서는 그런거 없이 출력한 것 같다.

(추가.. 다시 생각해보니 내가 바보 같았다. 어차피 NULL이 있는데 IF NULL을 해서 NULL처리 할거면 무슨 의미가 있었던건지 이제 알았다)
 
 이 문제를 풀면서 MYSQL의 JOIN 종류에 대해서 알아봤는데, FULL OUTER JOIN 기능이 없다는 것을 확인하였다. 관련해서 찾아보니까 LEFT JOIN과 RIGHT  JOIN 을 합해서 쿼리를 짜야 한다고 하는데, 확인해봐야할 것 같다.
 
 
▼ 참고자료

No. 내용 비고
1 Easy Approach 🔥|| LEFT JOIN 🤩 || USING() ✅ || Simple Explanation 👍 - Combine Two Tables - LeetCode kartikeymish
2 [MySQL] Join의 종류 leesomyoung (smlee) - velog
3 [SQL] SQL JOIN 종류 총 정리 (LEFT, RIGHT, FULL, INNER) 공부기록장
4 - -
5 - -

 

▼ 복습/히스토리

더보기
No. 복습일 비고
1    
2    
3    
4    
5    

- 작성코드

--

 

 

'DB & SQL > Leetcode' 카테고리의 다른 글

[LeetCode/MYSQL] - 182. Duplicate Emails  (0) 2023.06.29
[LeetCode/MYSQL] - 595. Big Countries  (0) 2023.06.28
[LeetCode/MYSQL] - 620. Not Boring Movies  (0) 2023.06.28