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

unit 2 SQL JOIN final

Uploaded by

hod cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

unit 2 SQL JOIN final

Uploaded by

hod cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL JOINS (⋈)

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

We can classify joins basically into two types:

1. INNER JOINS: These joins are the one that has the tuples that satisfy some conditions and rest are
discarded. Return only matching rows. Enable a maximum of 256 tables to be joined at the same
time.Further they are classified as
 Theta join (θ)
 Equi join
 Natural join (⋈)
2. OUTER JOINS: These have all the tuples from either or both the relations.Return all matching rows,
plus nonmatching rows from one or both tables can be performed on only two tables or viewsat a

 Left outer join (⟕ )


time.Further they are classified as

 Right outer join (⟖)


 Full outer join (⟗)

SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables. An
equal sign (=) is used as comparison operator in the where clause to refer equality.

The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <, >=, <= along with
conditions.

Student relation: Enroll relation:


REGISTER_NO NAME REGISTER_NO COURSE_ID
1813001 Steve 1813001 C10
1813002 Tom 1813002 C20
1813003 Harry 1813004 C30

INNER JOINS
Theta join(⋈θ) – They have tuples from different relations if and only if they satisfy the theta condition, here
the comparison operators (≤, ≥, ˂, ˃, =, ̚ )come into picture.

(INNER) JOIN: Returns records that have matching values in both tables

Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Ex:
SELECT * FROM student1
INNER JOIN enroll
on student1.register_no = enroll.register_no;

REGISTER_NO NAME REGISTER_NO COURSE_ID


1813001 Aarav 1813001 C10
1813002 Brindha 1813002 C20
NATURAL JOIN

Natural join operation on two relationsmatches tupleswhose values are the same on all attribute names that are
commonto both relations.The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way
that, columns with the same name of associated tables will appear once only.

Select * from table1 natural join table 2;

selectA1, A2, . . . , An
fromr1 natural join r2 natural join . . . natural join rm
whereP;

Select * from student1 natural join enroll;

REGISTER_NO NAME COURSE_ID


1813001 Aarav C10
1813002 Brindha C20

CROSS JOIN or CARTESIAN PRODUCT :

The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the
number of rows in the second table if no WHERE clause is used along with CROSS JOIN.This kind of result
is called as Cartesian Product. The Cartesian Product operationresult contains all pairs of tuples from the two
relations,regardless of whether their attribute values match.

Select * from table1 cross join table 2;

REGISTER_NO NAME REGISTER_NO COURSE_ID


1813001 Aarav 1813001 C10
1813001 Aarav 1813002 C20
1813001 Aarav 1813004 C30
1813002 Brindha 1813001 C10
1813002 Brindha 1813002 C20
1813002 Brindha 1813004 C30
1813003 Harry 1813001 C10
1813003 Harry 1813002 C20
1813003 Harry 1813004 C30

OUTER:

 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right
table.For non matching rows in leftside table, the values will be marked as NULL in right side table.
 Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Ex:

REGISTER_NO NAME REGISTER_NO COURSE_ID


1813001 Aarav 1813001 C10
1813002 Brindha 1813002 C20
1813003 Harry NULL NULL

 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the
left table. For non matching rows in right side table, the values will be marked as NULL in left side
table.

Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Ex:
SELECT * FROM student1
RIGHT JOIN enroll
on student1.register_no = enroll.register_no;

REGISTER_NO NAME REGISTER_NO COURSE_ID


1813001 Aarav 1813001 C10
1813002 Brindha 1813002 C20
NULL NULL 1813004 C30

 FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table.It
extends with nulls those tuples from the left-hand-side relation that did not match with any from the
right-hand side relation, and adds them to the result. Similarly, itextends with nulls those tuples from
the right-hand-side relation that did not match with anytuples from the left-hand-side relation and adds
them to the result.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Ex:

SELECT * FROM student1


FULL OUTER JOIN enroll
on student1.register_no = enroll.register_no;

REGISTER_NO NAME REGISTER_NO COURSE_ID


1813001 Aarav 1813001 C10
1813002 Brindha 1813002 C20
NULL NULL 1813004 C30
1813003 Harry NULL NULL

You might also like