Join 3 or More Tables in SQL

Join 3 or more table in SQL

In this article, we will have a look at an important topic in SQL, how to perform Join when there are more than two tables given .

Typically, Joins or JOIN Clauses are used to combine rows or records from two or more tables on the basis of a common or related column between them. There are many types of Joins. It is recommended to learn about them before proceeding to have a clear idea.

To show the JOIN between 3 or more tables we will consider the following schema design with tables :

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Here, we have 4 tables DEPARTMENT, GENDER, PROJECT and EMPLOYEES. This design represents relationship of one parent table with other tables. The DEPARTMENT table contains the information of each Employee’s department. The GENDER Table contains information of each employee’s gender and the PROJECT table contains the details about the project an employee is a part of including the status of project. The Employee is the main table which establishes relationships between different tables.

The EMPLOYEES table has attributes ID and name for each employee. The DEPT_ID attribute gives us information about the Employee’s department. It acts as a FOREIGN KEY which references ID (PRIMARY KEY) of DEPARTMENT Table. Similarly, GENDER_ID and PROJECT_ID are foreign keys referencing ID column of GENDER and PROJECT tables respectively.

Let us look at the description of each table along with their schema definition:

DEPARTMENT

The table DEPARTMENT is shown below:

ID NAME
1OPERATIONS
2HUMAN RESOURCES
3 FINANCE

Let us look at how to create table DEPARTMENT and insert some records in query:

GENDER

The table GENDER is shown below:

IDTYPE
1MALE
2FEMALE
3OTHER

Let us create the table and insert the above data:

PROJECTS
IDNAMESTATUS
1IT SUPPORTONGOING
2ORIENTATIONCOMPLETED
3RISK ANALYSISONGOING

Let’s create Table PROJECTS with above records in query:

EMPLOYEES
IDNAMEDEPT_IDGENDER_IDPROJECT_ID
1Amanda Jones222
2Steve Madden313
3Robert Henry111
4Shawn Adams131
5Luke Shaw313

Let us create the above table :

Note: All the above mentioned queries are for MYSQL and ORACLE Server. For the SQL server we need to do modify INTEGER or NUMBER to INT. Also run the GO command after each Create Table statement and after all INSERT operations.

Joining the Tables to get desired result:

  1. Now, if we want to get Name of Employees, belonging to a Department along with their Gender, so we have to perform join between tables EMPLOYEES, DEPARTMENT and GENDER. The query will look like:

The Output of this query will be :

NAMEDEPARTMENTGENDER
Amanda JonesHUMAN RESOURCESFEMALE
Luke ShawFINANCEMALE
Robert HenryOPERATIONSMALE
Shawn AdamsOPERATIONSOTHER
Steve MaddenFINANCEMALE
Employees with their Department and Gender.

Here, We performed join on three tables and the result was ordered on the basis of EMPLOYEE ID.

2. Now, Suppose we want to retrieve Records of Employees with their Department, Gender and the Project they are working on. The Join would look like this :

Output:

Employees with their Department, Gender and Project Name.

Now , let us write a query which will get us the Records of Employees of a Department with their Project Name and Project Status.

Output:

Employees of a Department with their Project and Status.

We order the result on basis of Department Name.

3. Now let’s write query to get the Number of Employees working in a Department and Project. We can use GROUP BY Clause for this, the query will be :

Output:

We grouped the result by Department name, Project , Count(*) return the number of rows in each group and ordered the final result set on basis of Department Name.

Join using Parent-Child Relationship

Another way to Join tables is using the Parent-Child Relationship . Let us have a quick look at how to obtain results from such a relation.

Here, we mainly use the WHERE Clause with instances of relation/table to join three or more tables. We need to create column as a primary key in one table and a foreign key in another table. Here, there is no need to explicitly use the ‘JOIN’ Keyword. The table containing the foreign keys is referred to as the ‘Child’ table. The table whose primary key is referred to from the child table is the ‘Parent’ table.

For the above example tables, the table EMPLOYEES is the Child Table containing the foreign keys which refer to the Primary Key of Parent Tables DEPARTMENT, GENDER and PROJECTS.

Now let us look at some queries to show the working:

For example like in Query 2 discussed above if we want to retrieve Records of Employees with their Department, Gender and the Project the query using the Parent- Child Relationship will look like this:

Output:

Similarly for Query 3 to get the Records of Employees of a Department with their Project Name and Project Status the query will be :

OUTPUT:

So that’s it for the article, you can try out the given examples in your SQL Compiler ( MYSQL, ORACLE or SQL Server), along with other examples to have a clear idea.

You can leave your suggestions or doubts in the comment section below.

Related Posts

  • Find 2nd Highest Salary Employee in SQL
    22 March

    Find 2nd Highest Salary of Employee in SQL

    In this article, we will look at a popular question How to find 2nd Highest Salary Employee in SQL asked in many interviews. Problem Given a Table of Employees with Employee Details we have to find the Employee with the 2nd Highest Salary. We have to print the Employee details of the Employee or we […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.