[175_EASY] Combine Two Tables

2022. 9. 23. 16:28·SQL/leetcode

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| personId    | int     |
| lastName    | varchar |
| firstName   | varchar |
+-------------+---------+
personId is the primary key column for this table.
This table contains information about the ID of some persons and their first and last names.

 

Table: Address

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| addressId   | int     |
| personId    | int     |
| city        | varchar |
| state       | varchar |
+-------------+---------+
addressId is the primary key column for this table.
Each row of this table contains information about the city and state of one person with ID = PersonId.

 

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.

 

Example 1:

Input: 
Person table:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1        | Wang     | Allen     |
| 2        | Alice    | Bob       |
+----------+----------+-----------+
Address table:
+-----------+----------+---------------+------------+
| addressId | personId | city          | state      |
+-----------+----------+---------------+------------+
| 1         | 2        | New York City | New York   |
| 2         | 3        | Leetcode      | California |
+-----------+----------+---------------+------------+
Output: 
+-----------+----------+---------------+----------+
| firstName | lastName | city          | state    |
+-----------+----------+---------------+----------+
| Allen     | Wang     | Null          | Null     |
| Bob       | Alice    | New York City | New York |
+-----------+----------+---------------+----------+
Explanation: 
There is no address in the address table for the personId = 1 so we return null in their city and state.
addressId = 1 contains information about the address of personId = 2.

문제 조건 

Person 테이블에 있는 각 사람의 firstname, lastname, city, state를 출력하시오. personId가 Address 테이블에 없는 경우 city,state는 null값을 출력합니다.

 

문제 풀이

select firstName,
        lastName,
        city,
        state
from person p 
    left join address a on p.personId = a.personId

 

 

저작자표시 (새창열림)

'SQL > leetcode' 카테고리의 다른 글

[182_EASY] Duplicate Emails  (0) 2022.09.23
[181_EASY] Employees Earning More Than Their Managers  (0) 2022.09.23
[2394#_MEDIUM] Employees With Deductions  (0) 2022.09.21
[2388#_MEDIUM] Change Null Values in a Table to the Previous Value  (0) 2022.09.21
[2372#_MEDIUM] Calculate the Influence of Each Salesperson  (0) 2022.09.21
'SQL/leetcode' 카테고리의 다른 글
  • [182_EASY] Duplicate Emails
  • [181_EASY] Employees Earning More Than Their Managers
  • [2394#_MEDIUM] Employees With Deductions
  • [2388#_MEDIUM] Change Null Values in a Table to the Previous Value
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (325)
      • SQL (271)
        • programmers (27)
        • leetcode (198)
        • hacker rank (27)
        • solvesql (1)
        • 개념 (15)
      • etc (28)
        • 1) (13)
        • 2) (14)
      • error (6)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    해커랭크
    프로그래머스
    직장인인강
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    HACKER_RANK
    Hard
    SQL
    패캠챌린지
    시계열데이터분석
    LeetCode
    MySQL
    solvesql
    파이썬을활용한시계열데이터분석
    패스트캠퍼스후기
    easy
    직장인자기계발
    group by
    패스트캠퍼스
    medium
    프리미엄
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[175_EASY] Combine Two Tables
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.