Joins
Joins
JOIN
• In relational databases, data is spread over
multiple tables. Sometimes we may want data
from two or more tables. A join is an
operation which combines results from two or
more tables
• A single SQL can manipulate data from all the
tables
Types of JOIN
• Cross Join / Cartesian Product
• Inner Join / Equi Join
• Outer Join
– Left Join
– Right Join
– Full Join
• Self Join
Cartesian Product Or Cross Join
Returns All rows from first table, Each row
from the first table is combined with all rows
from the second table
Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1, table2 as t2
INNER JOIN / EQUI JOIN
• Most commonly used joins
Includes only matching rows from both the tables where
there is a match OR An inner join between two (or more)
tables is the Cartesian product that satisfies the join
condition in the WHERE clause
ANSI-Style Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1 INNER JOIN table2 as t2 ON
t1.c1 = t2.c1
WHERE <predicate>
ORDER BY t1.c1, …, t1.cn, t2.c1, …, t2.cn
Theta-Style Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1, table2 as t2
WHERE t1.c1 = t2.c1
AND <predicate>
ORDER BY t1.c1, …, t1.cn, t2.c1, …, t2.cn
Emp(Emp_ID, City)
Customer(Cust_ID, City)