DB Presentation
DB Presentation
Presentation
TOPIC : JOIN
SUBJECT : DATABASE SYSTEM
PRESENTED BY : AUSHNA AQDAS
ZAINAB
KHADIJA
1
WHAT IS JOIN?
❖A JOIN IS USED TO
COMBINE RECORDS FROM
TWO OR MORE TABLES BY
USING VALUES COMMON TO
EACH.
3
Left Table
• The "left table" is the table that appears first in the join clause.
• In a query, it is the table mentioned on the left side of the JOIN keyword.
Right Table
• The "right table" is the table that appears second in the join clause.
• In a query, it is the table mentioned on the right side of the JOIN keyword.
LEFT RIGHT
TABLE TABLE
EXAMPLE:
Select ID, EmpName, Address From employee emp INNER JOIN employeeDetails
empdt ON (emp.iD=empdt.ID)
4
WHY WE USE JOINS?
Flexibility - It allow the user to access and manage records from more than one table.
Data Redundancy - Join allow us to keep data redundancy low so that we can reduce the
amount of data anomalies.
Efficiency – Execute faster and show results much more quickly than any other sub query.
5
TYPES OF
JOINS
• INNER JOIN
• OUTER JOIN
• SELF JOIN
• NATURAL JOIN
• CROSS JOIN
6
INNER JOIN
INNER JOIN
An INNER JOIN is a type of join that returns only the rows
that have matching values in both tables.
8
INNER JOIN Syntax
Here,
• Table1 and table2 - two tables that are to be joined
• Column1 and column2 - columns common to
in table1 and table2
9
Example : INNER JOIN
Here, the SQL command selects the specified rows from both tables if the values of
customer_id (of the Customers table) and customer (of the Orders table) are a match.
10
As you can see, INNER JOIN excludes all the rows that are not common between two
tables.
11
OUTER JOIN
OUTER JOIN
The FULL OUTER JOIN statement joins two tables based
on a common column. It selects records that have
matching values in these columns and the remaining rows
from both of the tables.
• Combines the results of both left and right outer joins.
• Returns all matched or unmatched rows.
• Includes tables on both sides of the join clause.
• Include all rows from one table (designated as the left
or right table) and matching rows from the other table.
13
TYPES OF OUTER JOIN
14
LEFT OUTER
JOIN
15
LEFT OUTER JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the
right side, if there is no match.
Left join returns all the row from left table and matching rows from the right
table.
• LEFT JOIN is used to retrieve data from two or more tables based on a related
column between them.
• It returns all records from the left table (table1), and the matched records from
the right table (table2).
• If there is no match, NULL values are returned from the right table.
16
LEFT JOIN Syntax
The syntax of the SQL FULL OUTER JOIN statement is:
Here,
17
Example: LEFT Join
Here, the SQL command combines data from the Customers and Orders tables.
The query selects the customer_id and first_name from Customers and the amount
from Orders.
Hence, the result includes rows where customer_id from Customers matches customer
from Orders.
18
Here's how this code works:
19
RIGHT OUTER
JOIN
20
RIGHT OUTER JOIN
21
RIGHT JOIN Syntax
The syntax of the SQL RIGHT OUTER JOIN statement is:
Here,
22
Example: RIGHT Join
Here, the SQL command selects the customer_id and first_name columns (from the
Customers table) and the amount column (from the Orders table).
And, the result set will contain those rows where there is a match between
customer_id (of the Customers table) and customer (of the Orders table), along with
all the remaining rows from the Orders table.
23
Here's how this code works:
24
FULL OUTER
JOIN
25
FULL OUTER JOIN
26
FULL OUTER JOIN SYNTAX
Here,
• Table1 and table2 are the tables to be joined
• Column1 and column2 are the related columns in the two tables
27
Example: FULL OUTER Join
Here, the SQL command selects the customer_id and first_name columns (from the
Customers table) and the amount column (from the Orders table).
The result set will contain all rows of both the tables, regardless of whether there is a
match between customer_id (of the Customers table) and customer (of the Orders
table).
28
29
SELF JOIN
30
SELF JOIN
A self join is a join in which a table is joined with itself
(which is also called unary relationship). To join a table itself
means that each row of the table is combined with itself and
with every other row of the table.
31
SELF JOIN Syntax
The syntax of the SQL SELF OUTER JOIN statement is:
Here,
32
Company a Company b
Example: SELF Joinemp_
id
emp_n
ame
manage city
r_id
emp_
id
emp_n
ame
manag
er_id
city
3 Hamza 4 Hassan
4 Hassan 1 Alee
33
NATURAL JOIN
34
Natural JOIN
35
NATURAL JOIN Syntax
The syntax of the NATURAL JOIN statement is:
SELECT columns: Specifies the columns you want to retrieve data from.
NATURAL JOIN: The keyword that performs the join based on columns with the
same name in both tables.
36
Student FEE
Roll Name Contact Roll No. Amount Paid
Example: NATURAL Join No.
1 JOHN 123456 1 5000 Yes
7
2 SAM 123456 2 4000 No
The SQL query for a natural join: 8
37
CROSS JOIN
38
Cross JOIN
39
Cross JOIN Syntax
The syntax of the Cross JOIN statement is:
SELECT: This specifies that you want to select all columns from the resulting
joined table.
FROM table1: Specifies the first table involved in the cross join operation. table1
is the alias for the first table .
CROSS JOIN table2: Specifies the second table involved in the cross join
operation. table2 is the alias for the second table.
40
Student FEE
ID Name ID Course
Example: Cross Join
1 Math
1 JOHN
2 SAM 2 History
The SQL query for a CROSS join: