๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๐ŸŒฎ ๐Ÿ’ฌ
๐Ÿ’ป leetcode/database

[LeetCode] Database | 175. Combine Two Tables

by ๋ฐ”์ฟ„๋ฆฌ 2021. 12. 29.

Problem


 

Example


 

Thinking


Output์„ ํ™•์ธํ•˜๋ฉด person table์— ์žˆ๋Š” ํšŒ์›๋“ค์„ ๋‚˜์—ดํ•˜๊ณ  ๊ทธ ํšŒ์›์— ํ•ด๋‹นํ•˜๋Š” (personId๊ฐ€ ๋™์ผํ•œ) city, state ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ…Œ์ด๋ธ”์„ ์™„์„ฑ์‹œ์ผฐ๋‹ค.

 

Address ์ •๋ณด๊ฐ€ ์—†๋Š” ํšŒ์›์ผ ๊ฒฝ์šฐ์—๋Š” Null๋กœ ํ‘œ์‹œ ํ•ด์ฃผ์—ˆ๋‹ค.

 

ํ…Œ์ด๋ธ”์ด 2๊ฐœ ์žˆ์œผ๋‹ˆ join์„ ํ•ด์ค„ ๊ฒƒ์ด๋ฉฐ, ์กฐ์ธ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ(Person table) ์ •๋ณด๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๊ธฐ์— LEFT JOIN์„ ์‚ฌ์šฉํ•ด๋ณด์ž.

 

Solve


์ œ์ถœ ๋‹ต์•ˆ

 

select p.firstName, p.lastName, a.city, a.state 
    from Person p left join Address a 
    on p.personId = a.personId

 


 


https://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