Database/SQL Server

SQL Server - 릿코드 데이터베이스 문제 175. Combine Two Tables //Leetcode Database Problem

청렴결백한 만능 재주꾼 2022. 7. 27. 03:42
반응형

175. Combine Two Tables

문제 출처:Leetcode

두개의 테이블이 주어진다.

Person, Address

 

.여기서 문제는 

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.

Return the result table in any order.

The query result format is in the following example.

해석하면,

Person  테이블에 있는 각 사람의 이름과 성, 사는 도시, 사는 주를 SQL Query를 이용하여 뽑아라. 만약 주소에 PersonId가 없으면 그냥 Null로 대신해라. 순서는 상관없이 보여주는 예제처럼 뽑길 바람.

예시,

예시 출처:Leetcode.com

 

두개 테이블 조인하면 될 듯하다.

사람 기준이니까 Person테이블을 from으로 잡고 거기에 조인으로 Address테이블을 덧붙여준다.

 

/* Write your T-SQL query statement below */
select p.firstName, p.lastName, a.city, a.state
  from person p
  left outer join address a on a.personId = p.personId

 


 

다행히 좋은 결과가 나왔다.

결과 출처: Leetcode.com

반응형