[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
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바