0% found this document useful (0 votes)
21 views42 pages

DB Presentation

Uploaded by

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

DB Presentation

Uploaded by

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

Minhaj University Lahore

Presentation

TOPIC : JOIN
SUBJECT : DATABASE SYSTEM
PRESENTED BY : AUSHNA AQDAS
ZAINAB
KHADIJA

COURSE INSTRUCTOR : MA’AM NAJAM-US-


SAHAR

1
WHAT IS JOIN?
❖A JOIN IS USED TO
COMBINE RECORDS FROM
TWO OR MORE TABLES BY
USING VALUES COMMON TO
EACH.

❖CREATES A NEW TABLE


WITH DATA FROM BOTH
TABLES
JOINS requires two or more tables

Table 1 Table 2 Table 3 OUTPUT


Field1 Field1 Field1 DEPENDS
Field2 + Field2 + Field2 = ON
THE TYPE
Field3 Field3 Field3 OF
JOIN

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

• FULL OUTER JOIN

• LEFT OUTER JOIN

• RIGHT 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.

• Include rows from the left table which have matching


rows from right table.

• Excludes Non-Matching Rows: Rows without a


match in either table are excluded from the result set.

Example: INNER JOIN


SELECT * FROM table_A INNER JOIN table_B ON
table_A.A=table_B.A;

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

• FULL JOIN (FULL OUTER JOIN)


• LEFT JOIN (LEFT OUTER JOIN)
• RIGHT JOIN(RIGHT 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,

table1 is the left table to be joined


table2 is the right table to be joined
column1 and column2 are the common columns in the two tables

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

The SQL RIGHT 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 the
right table.
Example: RIGHT OUTER JOIN

SELECT * FROM table_A


FULL OUTER JOIN table_B
ON table_A.A=table_B.A;

21
RIGHT JOIN Syntax
The syntax of the SQL RIGHT OUTER JOIN statement is:

Here,

table1 is the left table to be joined


table2 is the right table to be joined
column1 and column2 are the related columns in the two tables

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

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

Example: FULL OUTER JOIN

SELECT * FROM table_A


FULL OUTER JOIN table_B
ON table_A.A=table_B.A;

26
FULL OUTER JOIN SYNTAX

The syntax of the SQL FULL OUTER JOIN statement is:

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.

In SQL, the Self JOIN operation allows us to join a table


with itself, creating a relationship between rows within the
same table.

31
SELF JOIN Syntax
The syntax of the SQL SELF OUTER JOIN statement is:

Here,

columns - specifies the columns we want to retrieve


table1 T1 and table1 T2 - two instances T1 and T2 for the same table table1
JOIN - connects two tables and is usually followed by an ON command that
specifies the common columns used for linking the two tables.
condition - specifies the condition specifying how the two instances of the same
table should be joined

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

1 Alee 3 Lahore 1 Alee 3 Lahore


2 Ahmed 2 Lahore 2 Ahmed 2 Lahore
3 Hamza 4 Lahore 3 Hamza 4 Lahore
4 Hassan 1 Lahore
QUERY 4 Hassan 1 Lahore

SELECT a.emp_id As "Emp_ID",a.emp_name AS


"Employee Name", b.emp_id AS "Manager emp_ emp_na manager manager
ID",b.emp_name AS "Manager Name" FROM company id me _id _name
a, company b 1 Alee 3 Hamza
WHERE a.Manager_id = b.emp_id;
2 Ahmed 2 Ahmed

3 Hamza 4 Hassan

4 Hassan 1 Alee
33
NATURAL JOIN

34
Natural JOIN

A natural join is a type of join operation in databases that


combines rows from two tables based on a common column
between them. When performing a natural join, the database
engine automatically matches the columns with the same
name in both tables and returns the rows where the values in
those columns are equal.

• NATURAL JOIN is a join operation that combines tables


based on columns with the same name and data types in
both tables.

• It automatically matches these columns between the


tables without explicitly specifying the join condition.

35
NATURAL JOIN Syntax
The syntax of the NATURAL JOIN statement is:

SELECT columns: Specifies the columns you want to retrieve data from.

table1 and table2: The tables to be joined.

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

SELECT s.Name, s.Contact, f.Amount, f.Paid


FROM Student s The tables were joined based on the common
NATURAL JOIN Fee f; Roll No column.

Name Contact Amount Paid

JOHN 1234567 5000 YES

SAM 1234568 4000 NO

37
CROSS JOIN

38
Cross JOIN

In SQL, the CROSS JOIN operation allows us to combine rows


from two or more tables without any specific relationship between
them.
• A Cross Join combines each row of the first table with each row
of second table.
• It is a basic type of inner join that is used to retrieve the
Cartesian product (or cross product) of two individual tables.

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:

SELECT s.ID, s.Name, f.ID, f.Course


FROM Students The cross join will produce the following
CROSS JOIN Courses; result:
ID NAME ID COUR
SE
1 JOHN 1 Math
1 JOHN 2 History
2 SAM 1 Math
2 SAM 2 History
41
THANK
YOU

You might also like