Ex - 5 Joins
Ex - 5 Joins
Aim:
To Query the database tables and explore natural, equi and outer joins.
INNER JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
Sample Table
EMPLOYEE
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
Syntax
FROM table1
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
Syntax
FROM table1
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
Syntax
FROM table1
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
Syntax
FROM table1
ON table1.matching_column = table2.matching_column;
Query
FROM EMPLOYEE
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
What is Natural Join in SQL?
Suppose we have two tables − one which stores the personal information of employees along with their department
ID and the other stores the departments' details. How do we bring the data from two different tables into a single
table? The answer is Joins.
Joins in SQL are used to combine data from more than one table based on a join condition and view them together as
a single table. We have various types of joins in SQL and this article will focus on Natural Join.
Natural Join in SQL combines records from two or more tables based on the common column between them. The
common column must have the same name and data type in both the tables. SQL joins the tables based on this common
column and hence, we do not need to explicitly specify the join condition.
There is no need to use the ON clause for join condition in natural join.
There will always be unique columns in the output of a natural join.
Syntax
SELECT * FROM
tableA NATURAL JOIN tableB
Example of Natural Join in SQL
Let us create two tables’ employee and department and insert some data into it. We will then implement natural
join on these two tables.
Output
Output
Code
Output
Natural inner join only displays records with a common department ID. What if I want to include the departments which
do not have an employee yet? Let us look at some examples which combine natural join with left, right, and full outer
joins in SQL.
Natural left join displays every single record from the left table, irrespective of whether the common column
value (here, DeptID) occurs in the right table or not.
Syntax
SELECT * FROM
tableA NATURAL LEFT JOIN tableB
Code
Output
The department ID D3000 is not present in department table. Hence the details of Shreya whose department ID
is D3000 were not available in the natural join output. In the above example though, Shreya’s records are available
since natural left join displays all the records from the left (employee) table.
lOMoARcPSD|30185622
Natural right join is similar to natural left join but here, every record from the right table will be present in the output.
Suppose there is a new Finance department but it does not have any employees yet. We can still see its details in the
joined table using natural right join!
Syntax
SELECT * FROM
tableA NATURAL RIGHT JOIN tableB
Code
Output
Explanation
We have an extra row for the Finance department with ID D4002, when compared to the natural join example. Since
no employee has joined this department yet, the employee details are all null.
lOMoARcPSD|30185622
Natural full join is like a union of both input tables. If a given DeptlD is not available in the other table, the missing data
will be filled with null values in the output.
Syntax
SELECT * FROM
tableA NATURAL FULL JOIN tableB
Code:
Output:
Syntax
SELECT * FROM
tableA NATURAL JOIN tableB
WHERE filterCondition
lOMoARcPSD|30185622
Code
Output
Explanation We first create a natural join of the employee and department tables. Then we filter out those records
from the resultant table where the department name is Technology.
We can join more than two tables using natural join in SQL. We already have two tables employee and department.
Let us create another table address which will store the location city and state of each department.
Output:
| Location | State |
|: :|: :|
| Bangalore | Karnataka |
| Hyderabad | Telangana |
| Gurugram | Haryana |
| Mumbai | Maharashtra |
lOMoARcPSD|30185622
lOMoARcPSD|30185622
Syntax
SELECT * FROM
tableA NATURAL JOIN tableB NATURAL JOIN tableC
Code
SELECT * FROM
employee NATURAL JOIN department NATURAL JOIN address
Output
Equi join in SQL is a type of Inner Join in which two or more tables are joined by equating the common column values
of the tables. The table participating in Equi join should have at least one common column on which equality operation
can be performed.
Syntax
The Equi join in SQL can be written with the WHERE clause and also the ON clause for equating the columns of the
participating tables.
Example
with ON clause
> SELECT *
FROM Table1
JOIN Table2
ON Table1.ColumnName = Table2.ColumnName;
For example - Let's understand Equi join with the help of two tables.
lOMoARcPSD|30185622
1. Products table
id product_name
1 Iwatch
2 IPhone
3 IPod
4 macbook
2. Discount table
id dicount_percentage
2 20%
4 17%
Suppose we have a need to know the discounts available on the products then can we only know that by the Product
table or the discount table? No, we have to combine both the table to know what are the discounts available on the
products.
With the help of inner join, we can join both the products table and the discount table to know on which products the
discount exists. The column on which we can perform Equi join is the id column in both tables.
The resultant table after performing Equi join on products and the discount table will be −
SELECT *
FROM Products, Discount
WHERE Products.id = Discount.id;
d product_name id discount_percentage
2 Iphone 2 20%
4 macbook 4 17%
lOMoARcPSD|30185622