304 North Cardinal St.
Dorchester Center, MA 02124

# Combine Two Tables LeetCode Solution

## Problem – Combine Two Tables LeetCode Solution

SQL Schema

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    |
+-------------+---------+
| 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       |
+----------+----------+-----------+
+-----------+----------+---------------+------------+
| 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.
``````

## Combine Two Tables LeetCode Solution in MySQL

``````SELECT FirstName, LastName, City, State
FROM Person LEFT JOIN Address USING (PersonId)
``````

## Combine Two Tables LeetCode Solution in MS SQL Server

``````select p.FirstName, p.LastName, a.City, a.State
from Person as P left join Address a on a.PersonId = p.PersonId

``````

## Combine Two Tables LeetCode Solution in Oracle

``````SELECT firstName, lastName, city, state FROM Person p LEFT JOIN Address a ON p.personId = a.personId;
``````
##### Combine Two Tables LeetCode Solution Review:

In our experience, we suggest you solve this Combine Two Tables LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Combine Two Tables LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Combine Two Tables LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions