Data & BI/SQL Coding Test

[LeetCode](초급) 175.Combine Two Tables

무료한하늘 2025. 5. 16. 10:48
728x90

문제

Column Name Type
personId int
lastName varchar
firstName varchar
[Table : Person]
personId is the primary key (column with unique values) for this table.
This table contains information about the ID of some persons and their first and last names.

 

 

Column Name Type
addressId int
personId int
city varchar
state varchar
[Table : Address]
addressId is the primary key (column with unique values) for this table.
Each row of this table contains information about the city and state of one person with ID = PersonId.

 

Write a solution 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.

 

Person 테이블의 각각 사람의 first name, last name, city, state 를 출력해라.

정보가 없다면 null 로 대신 출력해라.

 

풀이

SELECT a.firstName, a.lastName, b.city, b.state
FROM Person a
LEFT OUTER JOIN Address b ON a.personId = b.personId

 

출력결과

firstName lastName city state
Allen Wang null null
Bob Alice New York City New York

 

300x250