Estimated reading time: 4 minutes
When running an sql select query, most likely we may have a need to bring in data from other tables.
The reason for this is that tables have a specific purpose for the data they store.
One of the most important features of a data architecture is to avoid duplication.
This has the impact of only storing data on a table that is required.
As a result, joining tables is necessary if you want to get all the information you need .
As an illustration of customer purchase, the purchase details would be on the purchase table.
BUT the customer details would not be stored there. They would be stored on the customer table.
To put it another way, a customer may make many purchases, but you only need their name and address etc. stored once.
The object therefore of joining tables is to use this structure, and avoid duplication over many tables .
For example in the above table, if you had the customer name and address on the purchases table and customer table,
they would always have to be in sync, and it would bring up a maintenance headache for the database administrator.
So what was developed was primary and foreign keys, which help to join tables, based on unique similar values in a column in each table.
What are the types of joins that can be used?
Inner Join: An inner join is where you join two tables but only return the rows that match in both tables.
Left Join: A left join is where you return all the records from the left table, and any matched records from the right table.
Right Join: A right join is where you return all the records from the right table, and any matched records from the left table.
Full Join: A full join uses the left join and right join, and returns all the rows from both tables in the output.
Equally important, where there is no value found a NULL value will be returned.
Lets take some examples to explain the above concepts.
So we have two tables below:
Sales Database, Customer Table
Sales Database, Sales Table
If we run this code, we will return the below. For this reason, the purpose of an Inner Join is to only return matched records common to both.
select a.customer_no, a.customer_type, b.INVOICE_NO from dbo.CUSTOMER a -- left table inner join sales.dbo.sales b -- right table on a.CUSTOMER_NO = b.CUSTOMER_NO
With a left join of the below code will return four rows. All the records from the left table and any matched records from the right table.
select a.customer_no, a.customer_type, b.INVOICE_NO from dbo.CUSTOMER a --left table left join sales.dbo.sales b --right table on a.CUSTOMER_NO = b.CUSTOMER_NO
With a right join of the below code will return three rows. All the records from the right table and any matched records from the left table.
select a.customer_no, a.customer_type, b.INVOICE_NO from dbo.CUSTOMER a --left table right join sales.dbo.sales b -- right table on a.CUSTOMER_NO = b.CUSTOMER_NO
Finally, in order to run a full join, the result of the below code would be as per the below. The objective is to return all rows matching and unmatching.
In the output, there is a Null Value, meaning that that row has no value for INVOICE_NO which is a primary key.
In essence, primary keys are not allowed to have null values.
select a.customer_no, a.customer_type, b.INVOICE_NO from dbo.sales b full join sales.dbo.customer a on a.CUSTOMER_NO = b.CUSTOMER_NO