0% found this document useful (0 votes)
35 views

DBMS Exp 6

The document discusses various SQL JOIN operations. It defines JOIN as combining records from two tables based on a common column. The main JOIN types are CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN. Examples of JOIN queries are provided to extract data from two sample tables - COMPANY and DEPARTMENT - based on different JOIN clauses. The aim of the experiment was to implement various JOIN operations to combine data from multiple tables.

Uploaded by

Aman Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

DBMS Exp 6

The document discusses various SQL JOIN operations. It defines JOIN as combining records from two tables based on a common column. The main JOIN types are CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN. Examples of JOIN queries are provided to extract data from two sample tables - COMPANY and DEPARTMENT - based on different JOIN clauses. The aim of the experiment was to implement various JOIN operations to combine data from multiple tables.

Uploaded by

Aman Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name: Aman Chavan

PRN: 120A7016
Div: B(SE-ECS)
EXPERIMENT NO. 6
AIM: Implement various Join operations.
.

OBJECTIVES: To perform various SQL JOIN clause to extract data from 2 (or
more) tables.
Joins clause is used to combine records from two or more tables in a database. A JOIN is a
means for combining fields from two tables by using values common to each.
Join is a combination of a Cartesian product followed by a selection process. A Join
operation pairs two tuples from different relations, if and only if a given join condition
is satisfied.

Join Types are −

• The CROSS JOIN


• The INNER JOIN
• The LEFT OUTER JOIN
• The RIGHT OUTER JOIN
• The FULL OUTER JOIN

The CROSS JOIN

A CROSS JOIN matches every row of the first table with every row of the second table.
If the input tables have x and y columns, respectively, the resulting table will have x+y
columns. Because CROSS JOINs have the potential to generate extremely large tables,
care must be taken to use them only when appropriate.

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
CROSS JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

Department of EXTC, SIES GST


Consider two tables, COMPANY and DEPARTMENT.
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);

SELECT EMP_ID, NAME, DEPT FROM COMPANY CROSS JOIN


DEPARTMENT;

INNER JOINS
A INNER JOIN creates a new result table by combining column values of two tables
(table1 and table2) based upon the join-predicate. The query compares each row of
table1 with each row of table2 to find all pairs of rows, which satisfy the join-
predicate.
An INNER JOIN is the most common type of join and is the default type of join. You
can use INNER keyword optionally.
The following is the syntax of INNER JOIN –
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;

EXAMPLE
SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

OUTER JOINS

Department of EXTC, SIES GST


An inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to include all
the tuples from the participating relations in the resulting relation. There are three kinds
of outer joins − left outer join, right outer join, and full outer join.

LEFT OUTER JOIN:

This join returns all the rows of the table on the left side of the join and matching
rows for the table on the right side of join. The rows for which there is no matching
row on right side, the result-set will contain null. LEFT JOIN is also known as LEFT
OUTER JOIN.
Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.
SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN
DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

RIGHT OUTER JOIN:


RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on
the right side of the join and matching rows for the table on the left side of join. The

Department of EXTC, SIES GST


rows for which there is no matching row on left side, the result-set will contain null.
RIGHT JOIN is also known as RIGHT OUTER JOIN.

Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are same.
SELECT EMP_ID, NAME, DEPT FROM COMPANY RIGHT OUTER JOIN
DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

FULL OUTER JOIN


FULL JOIN creates the result-set by combining result of both LEFT JOIN and
RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.

Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.

Department of EXTC, SIES GST


table2: Second table
matching_column: Column common to both the tables.

SELECT EMP_ID, NAME, DEPT FROM COMPANY FULL OUTER JOIN


DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

Department of EXTC, SIES GST


Code:

Department of EXTC, SIES GST


Department of EXTC, SIES GST
Department of EXTC, SIES GST
Department of EXTC, SIES GST
Conclusion: We successfully implemented various joint operations.

Department of EXTC, SIES GST

You might also like