Combining two tables is a very simple and easy SQL question that is being asked in many FAANG companies.
There are two tables containing data related to one column.
SQL Schema for the first table named Person:
CREATE TABLE IF NOT EXISTS Person (personId int, firstName varchar(255), lastName varchar(255))
INSERT INTO Person (personId, lastName, firstName) values ('1', 'Wang', 'Allen')
INSERT INTO Person (personId, lastName, firstName) values ('2', 'Alice', 'Bob')
personId | firstName | lastName |
---|---|---|
1 | Wang | Allen |
2 | Alice | Bob |
SQL Schema for the second table named Address:
CREATE TABLE IF NOT EXISTS Address (addressId int, personId int, city varchar(255), state varchar(255))
INSERT INTO Address (addressId, personId, city, state) values ('1', '2', 'New York City', 'New York')
INSERT INTO Address (addressId, personId, city, state) values ('2', '3', 'California', 'California')
addressId | personId | city | state |
---|---|---|---|
1 | 2 | New York City | New York |
2 | 3 | California | California |
firstName | lastName | city | state |
---|---|---|---|
Allen | Wang | Null | Null |
Bob | Alice | New York City | New York |
SELECT firstName, lastName, city, state FROM Person LEFT JOIN Address.personId=Person.personId;
0 Comments