Updated March 10, 2023
Introduction to SQL Multiple Join
We can perform multiple joins in a particular query statement that retrieves the data by combining the records of more than one table. Depending on how we have to retrieve the data and what should be the behavior while retrieving and matching the records we can make the use of various types of joins that are available in SQL. Whenever we perform more than one join of the same of a different type in a single query statement, it is said that we are making the use of multiple joins. In this article, we will study the general syntax of multiple joins and implement them with the help of a certain example.
Types of Joins
There are different types of joins such as inner join, outer join, left inner join, right inner join, left outer join, and right outer join. Their behavior differs in the number and consideration of the records that are retrieved by combining the records of right and left tables. The following figure demonstrates the type of joins and its behavior. In multiple joins, we can make the use of either, some or all of the types of joins according to our necessity.
Syntax of Multiple Joins:
The syntax of the Multiple Joins is as shown below –
SELECT table name1.column name1,table name1.column name2,table name2.column name1,....
FROM table name1
(LEFT/RIGHT) JOIN table name2
ON table name1.matching_column name = table name2. matching_column name
(LEFT/RIGHT) JOIN table name3
ON table name1.matching_column name = table name3. matching_column name
(LEFT/RIGHT) JOIN table name4
ON table name1.matching_column name = table name4. matching_column name
...;
In the above syntax, it is not necessary that every table’s join should be applied with the first table itself. We can perform joins on three or more tables to perform multiple joins. The type of joins can also vary or remain the same, for example, we can perform multiple inner joins between the tables or perform some joins on tables as inner join while other as left join, etc.
Examples of SQL Multiple Join
Consider an example where we are trying to maintain the customer-related data of arrival and exit for a particular shop selling some items that have certain attributes attached to it. The data related to customer activity of arriving and exiting is stored inside the table named crm_main that has many columns in it such as own code which is a unique auto-incremented identifier assigned to the activity of the customer. Further, to store the tenant and company details their’s tenant code and comp code and the financial year in which the transaction is recorded is identified by the finyearcode. The main part stored here is the activity and subactivity which helps us determine whether that customer has arrived or exited. The contents and structure of crm_main table are as shown in the output of below query –
SELECT * FROM crm_main
The execution of the above query statement gives an output which is as shown below –
There are certain details of the items for purchasing which the customer has arrived or exited. This item details for which the customer has arrived is stored in the table crm_item. The own code in the table crm_main is stored as crmMainOwnCode in the table crmitem. Each crm_item is assigned a unique identifier column value named own code. The contents of the table crm_item as shown below –
SELECT * FROM crm_item;
The execution of the above query statement gives an output which is as shown below –
Certain things should be recorded for the customer’s activity and the item for which that customer has performed that activity such as the purpose of visit, design type required, inward type whether for ordering he new item, or buying the existing manufactured item. All these things are stored in the table crm_attributes as shown in the output of below query –
SELECT * FROM crm_attributes;
The execution of the above query statement gives an output which is as shown below –
The own code of crm_main table is stored as crmMainOwnCode while the own code of crm_item table is stored as crmItemOwnCode in the table crm_attributes.
Now, what we have to do is to calculate ItemWise ReasonWise Customer Walkout Count for only exit activity to recognize what is the count of exiting the customers. For this, we will make the use of the following query statement that involves multiple joins on the tables crm_mian, crm_item, and crm_attributes and mentioning the date range between which we have to find the exit count by mentioning the subactivity as exit and status as 1. Also, we will retrieve the reason for which the item was canceled for purchasing by the user from the attributes by specifying the attrKey as “CancellationReason”. The item code, reason for cancellation and exit count are retrieved using the below query statement –
SELECT
COALESCE(citem.crmItemCode, 0) AS itemCode,
COALESCE(cattr.attrValue, 'No Reason') AS reasonDescription,
COALESCE(COUNT(citem.crmItemCode), 0) AS walkcnt
FROM
crm_main cmain
LEFT JOIN crm_item citem
ON cmain.tenantCode = citem.tenantCode
AND cmain.compCode = citem.compCode
AND cmain.ownCode = citem.crmMainOwnCode
LEFT JOIN crm_attributes cattr
ON cmain.ownCode = cattr.crmMainOwnCode
AND citem.ownCode = cattr.crmItemOwnCode
AND cattr.attrKey = "CancellationReason"
WHERE cmain.tenantCode = 1
AND cmain.compCode = 1
AND cmain.cancelFlag = 0
AND citem.status = 1
AND cmain.subActivity = "Exit"
AND cmain.documentDate BETWEEN "2020-01-01"
AND "2020-06-01"
The execution of the above query statement gives an output which is as shown below –
We can observe that for items with item code as 1 with reason excess making and the total exit count of customers being 85 as can be seen from the output.
Conclusion
We can make the use of multiple joins to retrieve the data from more than one table and have constraints and relation specified accordingly especially while retrieving summarized data. The use of multiple joins involves using more than two tables to retrieve the result set from the query. We can make the use of any type of joins while using multiple joins such as inner, left, and right joins. Note that the joins can be the same or different type in a particular query. Using multiple joins.
Recommended Articles
We hope that this EDUCBA information on “SQL Multiple Join ” was beneficial to you. You can view EDUCBA’s recommended articles for more information.