-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-combine-two-tables.sql
More file actions
40 lines (36 loc) · 1.23 KB
/
01-combine-two-tables.sql
File metadata and controls
40 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- ============================================
-- 1. Combine Two Tables
-- LeetCode #175
-- ============================================
-- Problem: Write a 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.
-- Table: Person
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | personId | int |
-- | lastName | varchar |
-- | firstName | varchar |
-- +-------------+---------+
-- Table: Address
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | addressId | int |
-- | personId | int |
-- | city | varchar |
-- | state | varchar |
-- +-------------+---------+
-- Solution: Use LEFT JOIN to keep all persons even if they don't have an address
SELECT
p.firstName,
p.lastName,
a.city,
a.state
FROM Person p
LEFT JOIN Address a ON p.personId = a.personId;
-- Why LEFT JOIN?
-- We need ALL persons regardless of whether they have an address.
-- LEFT JOIN keeps all rows from the left table (Person) and fills NULLs
-- for columns from the right table (Address) when there's no match.