Combine Two Tables
Asked at Google
Difficulty
Easy
Time
O(n + m)
Space
O(n + m)
Pattern
Database
Companies
1
Problem
You are given two database tables. Person holds one row per person, with columns personId, lastName and firstName. Address holds one row per known address, with columns addressId, personId, city and state. The personId column in Address points back to a row in Person.
Write a SQL query that returns firstName, lastName, city and state for every person in the Person table. If a person has no matching row in Address, their city and state must still appear in the result, filled with NULL.
In other words, no person may be dropped from the output just because they have no address. The rows can come back in any order.
Every person must appear
Constraints
- ●
personId is the primary key of the Person table - ●
addressId is the primary key of the Address table - ●
personId in Address is a foreign key referencing Person.personId - ●
Each person has at most one address row - ●
Result rows may be returned in any order
Examples
Person = [[1, "Wang", "Allen"], [2, "Alice", "Bob"]]
Address = [[1, 2, "New York City", "New York"], [2, 3, "Leetcode", "California"]][["Allen", "Wang", null, null], ["Bob", "Alice", "New York City", "New York"]]Person = [[1, "Martin", null], [2, null, null]]
Address = [[1, 2, null, null]][[null, "Martin", null, null], [null, null, null, null]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → |
How to Think About It
Left join is required because some people may not have an address. FROM Person LEFT JOIN Address ON Person.personId = Address.personId ensures all people appear even without a matching address row.
No self-join needed here. The two tables are joined by personId (foreign key). LEFT JOIN returns all rows from Person and matched rows from Address; unmatched columns are NULL.
SELECT specific columns: p.firstName, p.lastName, a.city, a.state. Use table aliases p and a to disambiguate if both tables had overlapping column names (they do not here, but it is best practice).
Why not INNER JOIN: INNER JOIN would exclude Bob (no matching address). LEFT JOIN preserves all people regardless of whether they have an address.
Optimal Approach
Step 1: FROM Person p LEFT JOIN Address a ON p.personId = a.personId.
Step 2: SELECT p.firstName, p.lastName, a.city, a.state.
Walkthrough: Person = [[1, Martin, null], [2, null, null]]. Address = [[1, 2, null, null]].
- Person 1: no matching address -> (Martin, null, NULL, NULL)
- Person 2: matching address -> (null, null, null, null)
Time: O(n + m) for the join. Space: O(n + m) for the result set.
LEFT JOIN keeps unmatched people
What Trips People Up in Real Interviews
Using INNER JOIN instead of LEFT JOIN. This drops people without addresses. The problem requires all people to appear, so LEFT JOIN is mandatory.
Joining on the wrong column. The foreign key is personId in both tables, but you must join Person.personId = Address.personId, not addressId.
Selecting addressId when the problem asks for city and state. Read the problem statement carefully for the exact output columns.
Forgetting that LEFT JOIN produces NULL for unmatched columns. The result will have NULL city and state for people without addresses. This is correct behavior, not an error.
Adding DISTINCT or GROUP BY unnecessarily. Since personId is a primary key in Person, each person appears at most once. No deduplication needed.
Solution Code
O(n + m) timeSELECT p.firstName, p.lastName, a.city, a.state
FROM Person p
LEFT JOIN Address a ON p.personId = a.personId;Frequently Asked Questions
What is the Combine Two Tables problem?
Write a SQL query to report the first name, last name, city, and state of each person. The Person table contains personId, lastName, and firstName. The Address table contains addressId, personId, city, and state. A person may not have an address, so use a LEFT JOIN.
How do you solve Combine Two Tables?
The optimal approach runs in O(n + m) time and O(n + m) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Combine Two Tables?
Combine Two Tables is asked at Google. It is a easy difficulty problem.
What are common mistakes on Combine Two Tables?
- Using INNER JOIN instead of LEFT JOIN. This drops people without addresses. The problem requires all people to appear, so LEFT JOIN is mandatory.
- Joining on the wrong column. The foreign key is personId in both tables, but you must join Person.personId = Address.personId, not addressId.
- Selecting addressId when the problem asks for city and state. Read the problem statement carefully for the exact output columns.
- Forgetting that LEFT JOIN produces NULL for unmatched columns. The result will have NULL city and state for people without addresses. This is correct behavior, not an error.
- Adding DISTINCT or GROUP BY unnecessarily. Since personId is a primary key in Person, each person appears at most once. No deduplication needed.