Combine Two Tables Problem
Description
LeetCode Problem 175.
Table: Person
1
2
3
4
5
6
7
8
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
1
2
3
4
5
6
7
8
9
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
1
FirstName, LastName, City, State
MySQL Solution
We can join the two tables to get the address information of a person. Considering there might not be an address information for every person, we should use outer join instead of the default inner join.
1
2
3
select Person.FirstName, Person.LastName, Address.City, Address.State
from Person
left join Address on Person.PersonId = Address.PersonId;
LeetCode Database - Easy
LeetCode 175
LeetCode 176
LeetCode 181
LeetCode 182
LeetCode 183
LeetCode 196
LeetCode 197
LeetCode 511
LeetCode 512
LeetCode 577
LeetCode 584
LeetCode 586
LeetCode 595
LeetCode 596
LeetCode 597
LeetCode 603
LeetCode 607
LeetCode 610
LeetCode 613
LeetCode 619
LeetCode 620
LeetCode 627
More LeetCode Database
MySQL Tutorials