0% found this document useful (0 votes)
111 views

Database Systems: SQL Joins

SQL joins are used to combine data from multiple tables. There are four main types of SQL joins: inner join, left outer join, right outer join, and full outer join. An inner join returns rows where the join condition is met in both tables. A left outer join returns all rows from the left table and matched rows from the right table. A right outer join is similar but returns all rows from the right table. A full outer join returns all rows from both tables with nulls used for missing matches.

Uploaded by

Adnan Asif
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)
111 views

Database Systems: SQL Joins

SQL joins are used to combine data from multiple tables. There are four main types of SQL joins: inner join, left outer join, right outer join, and full outer join. An inner join returns rows where the join condition is met in both tables. A left outer join returns all rows from the left table and matched rows from the right table. A right outer join is similar but returns all rows from the right table. A full outer join returns all rows from both tables with nulls used for missing matches.

Uploaded by

Adnan Asif
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/ 26

Lecture 7

Database Systems

SQL JOINS

1
INTRODUCTION
 SQL JOINS are used to retrieve data from multiple tables.

 There are 4 different types of SQL joins:

 SQL INNER JOIN (sometimes called simple join or Equi Join)

 SQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)

 SQL RIGHT OUTER JOIN (or sometimes called RIGHT

JOIN)
 SQL FULL OUTER JOIN (or sometimes called FULL JOIN)

2
Type 1- INNER JOIN

 It is the most common type of SQL join.

 It returns all rows from multiple tables where the join

condition is met.

3
INNER JOIN-SYNTAX

SELECT columns FROM table1


INNER JOIN table2
ON table1.column = table2.column;

4
INNER JOIN-VISUAL ILLUSTRATION

5
INNER JOIN-EXAMPLE
Suppliers Orders
supplier_id supplier_name order_id supplier_ order_dat
id e

500125 10000 2003/05/1


10000 IBM 2

10001 Hewlett Packard 500126 10001 2003/05/1


3
10002 Microsoft
500127 10004 2003/05/1
10003 NVIDIA 4

6
INNER JOIN-EXAMPLE
SELECT suppliers.supplier_id,

suppliers.supplier_name, orders.order_date

FROM suppliers INNER JOIN orders ON

suppliers.supplier_id = orders.supplier_id;

7
INNER JOIN-EXAMPLE

Query Result
supplier_id name order_date

10000 IBM 2003/05/12

10001 Hewlett Packard 2003/05/13

8
Type 2- LEFT OUTER JOIN
This type of join returns all rows from the LEFT-hand table

specified in the ON condition and only those rows from the

other table where the joined fields are equal (join condition is

met).

9
LEFT OUTER JOIN-SYNTAX

SELECT columns FROM table1 LEFT

[OUTER] JOIN table2 ON

table1.column = table2.column;

10
LEFT OUTER JOIN-VISUAL
ILLUSTRATION

11
LEFT OUTER JOIN-EXAMPLE
Suppliers Orders
supplier_id supplier_name order_id supplier_ order_dat
id e

500125 10000 2003/05/1


10000 IBM 2

10001 Hewlett Packard 500126 10001 2003/05/1


3
10002 Microsoft

10003 NVIDIA

12
LEFT OUTER JOIN-EXAMPLE
SELECT suppliers.supplier_id,

suppliers.supplier_name, orders.order_date FROM

suppliers LEFT OUTER JOIN orders ON

suppliers.supplier_id = orders.supplier_id;

13
LEFT OUTER JOIN-EXAMPLE
Query Result

supplier_id supplier_name order_date

10000 IBM 2003/05/12

10001 Hewlett Packard 2003/05/13

10002 Microsoft <null>

10003 NVIDIA <null>

14
Type 3- RIGHT OUTER JOIN
 This type of join returns all rows from the RIGHT-hand table

specified in the ON condition and only those rows from the

other table where the joined fields are equal (join condition is

met).

15
RIGHT OUTER JOIN-SYNTAX

SELECT columns FROM table1 RIGHT

[OUTER] JOIN table2 ON

table1.column = table2.column;

16
RIGHT OUTER JOIN-VISUAL
ILLUSTRATION

17
RIGHT OUTER JOIN-EXAMPLE
Suppliers Orders
supplier_id supplier_name order_id supplier_ order_dat
id e

500125 10000 2013/08/1


10000 Apple 2

10001 Google 500126 10001 2013/08/1


3

500127 10002 2013/08/1


4

18
RIGHT OUTER JOIN-EXAMPLE
SELECT orders.order_id, orders.order_date,

suppliers.supplier_name FROM suppliers

RIGHT OUTER JOIN orders ON

suppliers.supplier_id = orders.supplier_id;

19
RIGHT OUTER JOIN-EXAMPLE
Query Result
order_id order_date supplier_name

500125 2013/08/12 Apple

500126 2013/08/13 Google

500127 2013/08/14 <null>

20
Type 4- FULL OUTER JOIN

 This type of join returns all rows from the LEFT-hand table

and RIGHT-hand table with nulls in place where the join

condition is not met.

21
FULL OUTER JOIN-SYNTAX
SELECT columns FROM table1 FULL

[OUTER] JOIN table2 ON table1.column =

table2.column;

22
FULL OUTER JOIN-VISUAL
ILLUSTRATION

23
FULL OUTER JOIN-EXAMPLE
Suppliers Orders
supplier_id supplier_name order_id supplier_ order_dat
id e

500125 10000 2013/08/1


10000 IBM 2

10001 Hewlett Packard 500126 10001 2013/08/1


3
10002 Microsoft
500127 10004 2013/08/1
10003 NVIDIA 4

24
FULL OUTER JOIN-EXAMPLE
SELECT suppliers.supplier_id,

suppliers.supplier_name, orders.order_date FROM

suppliers FULL OUTER JOIN orders ON

suppliers.supplier_id = orders.supplier_id;

25
FULL OUTER JOIN-EXAMPLE
Query Result
supplier_id supplier_name order_date

10000 IBM 2013/08/12

10001 Hewlett Packard 2013/08/13

10002 Microsoft <null>

10003 NVIDIA <null>

<null> <null> 2013/08/14

26

You might also like