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

SQL JOINS

This document provides an overview of SQL Joins, which are used to fetch and combine data from two or more tables based on common values. It details various types of joins including Inner Join, Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, along with their syntax and examples. Each join type is explained with its functionality and output results from sample tables.

Uploaded by

sc21cs301097
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)
2 views

SQL JOINS

This document provides an overview of SQL Joins, which are used to fetch and combine data from two or more tables based on common values. It details various types of joins including Inner Join, Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, along with their syntax and examples. Each join type is explained with its functionality and output results from sample tables.

Uploaded by

sc21cs301097
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/ 19

DBMS UNIT 3 SQL

JOINS
Prepared By:
Ms. Barkha Namdev
SQL JOIN
 SQL Join is used to fetch data from two or more
tables.

 It is used for combining column from two or more


tables by using values common to both tables.

 JOIN Keyword is used in SQL queries for joining


two or more tables.
TYPES OF JOINS
 Following are the types of JOIN that we can
use in SQL:
 Inner
 Outer
 Left
 Right
INNER JOIN
 This is a simple JOIN in which the result is based
on matched data as per the equality condition
specified in the SQL query.

 Inner Join Syntax is,

 SELECT column-name-list FROM table-name1


INNER JOIN table-name2 WHERE table-
name1.column-name = table-name2.column-
name;
inner Join
inner Join
 Inner JOIN query will be,

 SELECT * from class INNER


JOIN class_info where class.id
= class_info.id
ID NAME
;
ID Address
 OUTPUT:
1 abhi 1 DELHI

2 adam 2 MUMBAI

3 alex 3 CHENNAI
Natural Join
 Natural Join is a type of Inner join which is based
on column having same name and same datatype
present in both the tables to be joined.
 The syntax for Natural Join is,

 SELECT * FROM table-


name1 NATURAL JOIN
table-name2;
Natural Join
Natural join query will be,
SELECT * from class NATURAL JOIN
class_info;
 OUTPUT ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI

 In the above example, both the tables being joined


have ID column(same name and same datatype), hence
the records for which value of ID matches in both the
tables will be the result of Natural Join of these two
tables.
Outer Join
 Outer Join is based on both matched and
unmatched data. Outer Joins subdivide further
into,
 Left Outer Join
 Right Outer Join
 Full Outer Join
Left Outer Join

 The left outer join returns a resultset table with


the matched data from the two tables and then the
remaining rows of the left table and null from
the right table's columns.
 Syntax for Left Outer Join is,
 SELECT column-name-list FROM table-name1

LEFT OUTER JOIN table-name2 ON table-


name1.column-name = table-name2.column-
name;Copy
 To specify a condition, we use the ON keyword with

Outer Join.
Left Outer Join

 Here is the class table, ID NAME


1 abhi
2 adam
3 alex
4 anu
5 ashish
 and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
Left Outer Join

 SELECT * FROM class LEFT


OUTER JOIN class_info ON
(class.id
ID = class_info.id);
NAME ID Address

1 abhi 1 DELHI

2 adam 2 MUMBAI

3 alex 3 CHENNAI

4 anu null null

5 ashish null null


Right Outer JOIN
 The right outer join returns a resultset table with
the matched data from the two tables being joined, then
the remaining rows of the right table and null for the
remaining left table's columns.

 Syntax for Right Outer Join is,

 SELECT column-name-list FROM


table-name1 RIGHT OUTER JOIN table-
name2 ON table-name1.column-name
= table-name2.column-name;
Right Outer JOIN
 Here is the class table, ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
 and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
Right Outer Join query will
be,
 SELECT * FROM class RIGHT OUTER
JOIN class_info ON (class.id =
class_info.id);
 OUTPUT:
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
null null 7 NOIDA
null null 8 PANIPAT
FULL OUTER JOIN
 The full outer join returns a resultset table with the matched
data of two table then remaining rows of both left table and
then the right table.

 Syntax of Full Outer Join is,

 SELECT column-name-list FROM


table-name1 FULL OUTER JOIN
table-name2 ON table-
name1.column-name = table-
name2.column-name;
FULL OUTER JOIN
 Here is the class table, ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
 and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
FULL OUTER JOIN
 SELECT * FROM class FULL OUTER JOIN
class_info ON (class.id = class_info.id);
 OUTPUT:

ID NAME ID Address

1 abhi 1 DELHI

2 adam 2 MUMBAI

3 alex 3 CHENNAI

4 anu null null

5 ashish null null

null null 7 NOIDA

null null 8 PANIPAT

You might also like