본문 바로가기
MySQL/문제풀이

[LeetCode] 175. Combine Two Tables

by MINNI_ 2021. 3. 22.

1. 문제

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+

PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+

AddressId is the primary key column for this table.

 

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

 

FirstName, LastName, City, State


2. 답

SELECT FirstName, LastName, City, State
FROM person
    LEFT JOIN address ON person.personid = address.personid

3. KEY POINT

 

  • LEFT JOIN : 모든 사람의 주소 정보가 없을 수 있으므로 INNER JOIN이 아닌 LEFT JOIN을 사용

 

leetcode.com/problems/combine-two-tables/

 

Combine Two Tables - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

댓글