0% found this document useful (0 votes)
29 views17 pages

Ex - 5 Joins

Uploaded by

Vilasini Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views17 pages

Ex - 5 Joins

Uploaded by

Vilasini Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Ex No: 5 Query the database tables and explore natural, equi and outer joins.

Aim:

To Query the database tables and explore natural, equi and outer joins.

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:

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

EMP_ID EMP_NAME CITY SALARY AGE


1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36
6 Marry Canada 600000 48
PROJECT

PROJECT_NO EMP_ID DEPARTMENT

101 1 Testing

102 2 Development

103 3 Designing

104 4 Development

1. INNER JOIN

Syntax

SELECT table1.column1, table1.column2, table2.column1,

FROM table1

INNER JOIN table2

ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME,

PROJECT.DEPARTMENT FROM EMPLOYEE

INNER JOIN PROJECT

ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;

Output

EMP_NAME DEPARTMENT

Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN

Syntax

SELECT table1.column1, table1.column2, table2.column1,

FROM table1

LEFT JOIN table2

ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME,

PROJECT.DEPARTMENT FROM EMPLOYEE

LEFT JOIN PROJECT

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

SELECT table1.column1, table1.column2, table2.column1,

FROM table1

RIGHT JOIN table2

ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME,

PROJECT.DEPARTMENT FROM EMPLOYEE

RIGHT JOIN PROJECT

ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;

Output

EMP_NAME DEPARTMENT

Angelina Testing

Robert Development

Christian Designing

Kristen Development
4. FULL JOIN

Syntax

SELECT table1.column1, table1.column2, table2.column1,

FROM table1

FULL JOIN table2

ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT

FROM EMPLOYEE

FULL JOIN PROJECT

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.

Create Employee Table

CREATE TABLE employee (


EmployeeID varchar(10),
FirstName varchar(50),
LastName varchar(50),
DeptID varchar(10)
);

Insert Records into Employee Table

INSERT INTO employee


VALUES("E62549", "John", "Doe", "D1001"),
("E82743", "Priya", "Sharma", "D3002"),
("E58461", "Raj", "Kumar", "D1002"),
("E95462", "Ravi", "", "D1001"),
("E25947", "Shreya", "P", "D3000"),
("E42650", "Jane", "Scott", "D3001")

We can view the employee table using SELECT query.

SELECT * FROM employee

Output

| EmployeeID | FirstName | LastName | DeptID |


|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 |
| E82743 | Priya | Sharma | D3002 |
| E58461 | Raj | Kumar | D1002 |
| E95462 | Ravi | | D1001 |
| E25947 | Shreya | P | D3000 |
| E42650 | Jane | Scott | D3001 |

Create department table

CREATE TABLE department (


DeptID varchar(10),
DeptName varchar(40),
Location varchar(40)
);
Insert records into department table

INSERT INTO department


VALUES("D1001", "Technology", "Bangalore"),
("D1002", "Technology", "Hyderabad"),
("D3001", "Sales", "Gurugram"),
("D3002", "Operations", "Hyderabad"),
("D4002", "Finance", "Mumbai")

We can view the department table using SELECT query.

SELECT * FROM department

Output

| DeptID | DeptName | Location |


|: :|: :|: :|
| D1001 | Technology | Bangalore |
| D1002 | Technology | Hyderabad |
| D3001 | Sales | Gurugram |
| D3002 | Operations | Hyderabad |
| D4002 | Finance | Mumbai |

Natural Join on Employee and Department Tables

Code

SELECT * FROM employee NATURAL JOIN department

Output

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location |


|: :|: :|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
| E82743 | Priya | Sharma | D3002 | Operations | Hyderabad |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
| E42650 | Jane | Scott | D3001 | Sales | Gurugram |
lOMoARcPSD|30185622

Types of Natural Join in SQL

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 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

SELECT * FROM employee NATURAL LEFT JOIN department

Output

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location |


|: :|: :|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
| E82743 | Priya | Sharma | D3002 | Operations | Hyderabad |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
| E25947 | Shreya | P | D3000 | null | null |
| E42650 | Jane | Scott | D3001 | Sales | Gurugram |

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 in SQL

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

SELECT * FROM employee NATURAL RIGHT JOIN department

Output

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location |


|: :|: :|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
| E82743 | Priya | Sharma | D3002 | Operations | Hyderabad |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
| E42650 | Jane | Scott | D3001 | Sales | Gurugram |
| null | null | null | D4002 | Finance | Mumbai |

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 in SQL

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:

SELECT * FROM employee NATURAL FULL JOIN department

Output:

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location |


|: :|: :|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
| E82743 | Priya | Sharma | D3002 | Operations | Hyderabad |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
| E25947 | Shreya | P | D3000 | null | null |
| E42650 | Jane | Scott | D3001 | Sales | Gurugram |
| null | null | null | D4002 | Finance | Mumbai

Natural Join with WHERE Clause

Syntax

SELECT * FROM
tableA NATURAL JOIN tableB
WHERE filterCondition
lOMoARcPSD|30185622

Code

SELECT * FROM employee NATURAL JOIN department


WHERE DeptName = "Technology"

Output

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location |


|: :|: :|: :|: :|: :|: :|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |

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.

Natural Join using Three Tables

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.

Create address table

CREATE TABLE address (


Location varchar(40),
State varchar(40)
);

Insert records into the address table

INSERT INTO address


VALUES("Bangalore", "Karnataka"),
("Hyderabad", "Telangana"),
("Gurugram", "Haryana"),
("Mumbai", "Maharashtra")

We can view the address table using the SELECT query.

SELECT * FROM address

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

| EmployeeID | FirstName | LastName | DeptID | DeptName | Location | State


|
|: :|: :|: :|: :|: :|: :|:
--:|
| E62549 | John | Doe | D1001 | Technology | Bangalore |
Karnataka |
| E82743 | Priya | Sharma | D3002 | Operations | Hyderabad |
Telangana |
| E58461 | Raj | Kumar | D1002 | Technology | Hyderabad |
Telangana |
| E95462 | Ravi | | D1001 | Technology | Bangalore |
Karnataka |
| E42650 | Jane | Scott | D3001 | Sales | Gurugram | Haryana
|
lOMoARcPSD|30185622

What is Equi Join in SQL?

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 WHERE clause


> SELECT *
FROM Table1, Table2
WHERE TableName1.ColumnName = TableName2.ColumnName;

 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

You might also like