Combine Two Tables in SQL

Samuel AdesolaSamuel Adesola
2 min read

Overview

Structured Query Language) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS). Various database operations can be performed using SQL, one of the very common and important ones is Joining, i.e joining tables. There are also different types of join in SQL e.g outer join.

The task in this challenge is to 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. The given table contains information about the ID of some persons and their first and last names. Each row of the table contains information about the city and state of one person with ID = PersonId

Thought Process

In this requirement, we have to use Outer Joins in order to get the wanted results. This is because we want every row from the Person( which is we are specifying first using SELECT statement.) table so we are using LEFT JOIN on the contrary if we wanted every row from the address table we would have used RIGHT JOIN

From the given sample table, the PersonId in table Address is the foreign key of table Person, if we write a command to join these two tables, we should get the address information of a person.

Conclusion

Joining tables as mention above is one of the day to day activities of database administrator and knowing which type of join to use in different condition will help in improving productivity.

0
Subscribe to my newsletter

Read articles from Samuel Adesola directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samuel Adesola
Samuel Adesola