0% found this document useful (0 votes)
9 views25 pages

Joins

The document explains the concept of joins in relational databases, which combine data from multiple tables. It details various types of joins, including Cross Join, Inner Join, Left Join, Right Join, Full Join, and Self Join, along with their respective SQL syntax. Additionally, it provides examples of SQL queries for each type of join to illustrate their usage.

Uploaded by

rithike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Joins

The document explains the concept of joins in relational databases, which combine data from multiple tables. It details various types of joins, including Cross Join, Inner Join, Left Join, Right Join, Full Join, and Self Join, along with their respective SQL syntax. Additionally, it provides examples of SQL queries for each type of join to illustrate their usage.

Uploaded by

rithike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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

• C1 in t1 is usually t1’s primary key


• C1 in t2 is a foreign key in that table
• C1 of t1 and C1 of t2 must have same data type and
for certain data types same width
LEFT JOIN / LEFT OUTER JOIN
Includes all rows from left table regardless of
whether a match exist, and matching rows
from the right table
ANSI-Style Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1 LEFT JOIN table2 as t2 ON
t1.c1 = t2.c1
WHERE <predicate>
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>
RIGHT JOIN / RIGHT OUTER JOIN
Includes all rows from right table regardless of
whether a match exist, and matching rows
from the left table
ANSI-Style Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1 RIGHT JOIN table2 as t2
ON t1.c1 = t2.c1
WHERE <predicate>
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>
FULL JOIN /FULL OUTER JOIN
Includes all rows from both table regardless of
whether a match exist
ANSI-Style Syntax :
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1 FULL OUTER JOIN table2 as
t2 ON t1.c1 = t2.c1
WHERE <predicate>
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>
UNION
SELECT t1.c1, …, t1.cn, t2.c1, …, t2.cn
FROM table1 as t1, table2 as t2
WHERE t1.c1 (+) = t2.c1
AND <predicate>
Self join
• To join a table to itself
• In SELF JOIN two copies of the same table
have to be opened in memory. In FROM
clause the table name needs to be mentioned
twice. As the table names are same, the
second table will overwrite the first table and
it will result in only one table. To avoid this,
open each table using an alias
To list all the Employees along with their
Managers

SELECT emp.ENAME as [“Employee”],


mgr.ename as [“Manager”]
FROM employee emp INNER JOIN
employee mgr ON
emp.mgr = mgr.empno
Display the Names of all Customers who have
taken Loan

SELECT a.Cust_Id, b.Cust_Name,


FROM Customer_loan a INNER JOIN
customer_details b
ON a.cust_id = b.cust_id
List all cities of EMP if there is match in cities
in Customer & also unmatched Cities from
Emp

SELECT Emp.Emp_ID, Emp.City,


Customer.Cust_ID, Customer.City
FROM emp LEFT JOIN Customer
ON
emp .Emp.City = Customer .Customer.City
List all customer details and loan details if
they have availed loans

SELECT Customer_details.Cust_id, cname,


Loan_no, Amount
FROM Customer_details, LEFT OUTER JOIN
Customer_loan
ON Customer_details.Cust_id =
Customer_loan.Cust_id
List all cities of Customer if there is match in
cities in EMP & also unmatched Cities from
Customer

SELECT Emp.Emp_ID, Emp.City,


Customer.Cust_ID, Customer.City
FROM emp RIGHT JOIN Customer ON
emp .Emp.City = Customer .Customer.City
Example
SELECT t1.*, t2.* FROM Table1 as t1,
Table2 as t2
Example

Emp(Emp_ID, City)
Customer(Cust_ID, City)

• Get all combinations of emp and cust


information such that the emp and cust are
co-located
SELECT Emp.Emp_ID, Emp.City,
Customer.Cust_ID, Customer.City
FROM emp INNER JOIN Customer
ON
emp .Emp.City = Customer .Customer.City

You might also like