DBMS Exp 6
DBMS Exp 6
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.
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;
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
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;
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;
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
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;
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;