unit 2 SQL JOIN final
unit 2 SQL JOIN final
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
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
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.
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;
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.
selectA1, A2, . . . , An
fromr1 natural join r2 natural join . . . natural join rm
whereP;
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.
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:
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;
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: