Combine Two Tables
You are given two tables containing information about people: one with their basic personal details and another with their address information. Your task is to combine these two tables to create a single output that lists each person along with their full name and address. This is a common operation in data management and analysis, where you need to join related information from different sources.
Problem Description
You are provided with two tables: Person and Address.
The Person table contains the following columns:
personId: A unique identifier for each person (integer).firstName: The first name of the person (string).lastName: The last name of the person (string).
The Address table contains the following columns:
addressId: A unique identifier for each address (integer).personId: The identifier of the person to whom this address belongs (integer). This is a foreign key referencingpersonIdin thePersontable.street: The street name of the address (string).city: The city of the address (string).
Your goal is to write a query that returns a table with the following columns:
firstName: The first name of the person.lastName: The last name of the person.street: The street of the person's address.city: The city of the person's address.
You need to combine the information from both tables based on the personId.
Key Requirements:
- Retrieve the
firstName,lastNamefrom thePersontable. - Retrieve the
street,cityfrom theAddresstable. - Join the tables on the
personIdcolumn. - Ensure that even if a person does not have an address listed in the
Addresstable, they should still be included in the output, with theirstreetandcityfields beingNULL.
Edge Cases to Consider:
- A person might exist in the
Persontable but not have a corresponding entry in theAddresstable. - It is assumed that each
personIdin theAddresstable will have a correspondingpersonIdin thePersontable.
Examples
Example 1:
Input:
Person table:
| personId | firstName | lastName |
|---|---|---|
| 1 | John | Doe |
| 2 | Jane | Smith |
| 3 | Peter | Jones |
Address table:
| addressId | personId | street | city |
|---|---|---|---|
| 101 | 1 | Main St | Anytown |
| 102 | 2 | Oak Ave | Otherville |
Output:
| firstName | lastName | street | city |
|---|---|---|---|
| John | Doe | Main St | Anytown |
| Jane | Smith | Oak Ave | Otherville |
| Peter | Jones | NULL | NULL |
Explanation:
- John Doe (personId 1) has an address on Main St in Anytown.
- Jane Smith (personId 2) has an address on Oak Ave in Otherville.
- Peter Jones (personId 3) exists in the Person table but has no corresponding entry in the Address table, so their street and city are NULL.
Example 2:
Input:
Person table:
| personId | firstName | lastName |
|---|---|---|
| 1 | Alice | Wonderland |
Address table:
| addressId | personId | street | city |
|---|---|---|---|
| 201 | 1 | Rabbit Hole | Fableton |
| 202 | 1 | Queen's Castle | Wonderland |
Output:
| firstName | lastName | street | city |
|---|---|---|---|
| Alice | Wonderland | Rabbit Hole | Fableton |
| Alice | Wonderland | Queen's Castle | Wonderland |
Explanation:
- Alice Wonderland (personId 1) has two addresses listed. Both should be included in the output, duplicating her name and last name for each address.
Constraints
personIdinPersontable is unique.personIdinAddresstable is not necessarily unique (a person can have multiple addresses).- The number of rows in
Persontable can be up to 1000. - The number of rows in
Addresstable can be up to 5000. firstName,lastName,street, andcityare strings and can be empty.- Performance is important; the solution should be efficient for larger datasets.
Notes
- Consider which type of join operation is most suitable for this problem to handle cases where a person might not have an address.
- You will be writing a query to retrieve data, not modifying it.