Join in database in fact carries the same meaning as in English dictionary. It connects two tables in a database through a common column or key. If we want to find the customers who live in California and have bought Product A, we will need to āJoinā or connect the three tables(mentioned in my previous article) through common keys.The common key that connects Customers and Orders table is CustomerID and the common key that connects the Products table and Order table is ProductID.
In this particular scenario, since we are looking for customers who live in āCAā and bought Product āAā, (i.e. intersection of two data sets), we will be using āInner Joinā. Letās write the query, one step at a time. First, letās join the Customer table with Orders table.
Select a.CustomerID,a.State,b.ProductID
From Customers a
Join Orders b
On a.CustomerID=b.CustomerID
Where a.state='CA'
The query above will return the following rows.
Here, ProductID does not really give any information about the Product. However, ProductID is the common key between Orders Table and Products Table. And Product Table hold information about the product with ProductID 1. Let's join the Products table to the above query.
Select a.CustomerID,a.State,b.ProductID,p.Product
From Customers a
Join Orders b
On a.CustomerID=b.CustomerID
Join Products p
On b.ProductID = p.ProductID
Where a.state='CA'
And Product='A'
This above query will return the same number of records but notice the Product column (highlighted).
Try this exercise of using inner join with various small data sets. Write the SQL and then manually check against Venn diagram representing the data sets.
Now letās look at the second scenario where you want to find out what all customers living in California ordered.
Select a.CustomerID,a.State,b.ProductID,p.Product,b.Quantity
From Customers a
left Join Orders b
On a.CustomerID=b.CustomerID
left Join Products p
On b.ProductID = p.ProductID
Where state='CA'
Notice that you now see all 5 California customers in the list as well as the additional information of products each one have bought so far. Left join is primarily used to get additional optional information or to distinguish between customers who have certain data from those who do not. In this scenario we learned that 2 of 5 California customers have bought product A and the other three customers have not.
You may initially find it challenging to decide on when to use left join. In that scenario, visualize the tables or data set and Venn diagram. If we go back to Venn diagram, you see all the 5 CustomerID in the California set and CustomerID { 3,12} from the Product A set.
Now, letās look at the third scenario, where you want to see all the customers who live in California or have bought Product A. Ā āorā is represented as union in Set Theory which is equivalent to full outer join in database. As per the above diagram, Union of California and Product A will return all the CustomerID inside the two circles. {3,4,5,6,7,8,12}. The SQL query that represents the above scenario will look like this.
Select a.CustomerID,a.State,b.ProductID,p.Product,b.Quantity
From Customers a
full outer Join Orders b
On a.CustomerID=b.CustomerID
full outer Join Products p
On b.ProductID = p.ProductID
Where state='CA' or Product='A'
Resulting rows contains all California customers as well as customers who bought product āAā
With this, I will end this session. As I mentioned before, JOIN is the most important thing you need to learn for querying database. So, spend plenty of time writing queries for all the three join scenarios. Like Mathematics or Piano, the more you practice these joins, the more comfortable you will get. As we go along, I will show many more examples.