0% found this document useful (0 votes)
17 views26 pages

UNIT4 SQL Commands 14 Joins

Uploaded by

shindekrupa150
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)
17 views26 pages

UNIT4 SQL Commands 14 Joins

Uploaded by

shindekrupa150
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

STRUCTURED QUERY LANGUAGE

SQL COMMANDS(JOINS)

Prepared By
Chandni Shah
Lecturer, Government Polytechnic of Girls,
Ahmedabad
CONTENTS

Foreign key
Cross Join(Cartesian Product or Simple Join)
Inner Join
⚫ Equi Join
⚫ Non-equi Join
Self Join
Outer Join

2
FOREIGN KEY
In the relational databases, a foreign key is a
field or a column that is used to establish a link
between two tables.
In simple words you can say that, a foreign key in
one table used to point primary key in another
table.

3
FOREIGN KEY EXAMPLE

CID Name OID CID


1 Anita 1 2
2 Mita 2 2
3 Sita 3 3
4 1
Customer
Order
The “C_Id" column in the “Customers" table is the PRIMARY KEY in
the “Customers" table.
The “C_Id" column in the "Orders" table is a FOREIGN KEY in the
"Orders" table.
Here you see that “C_Id" column in the "Orders" table points to the
4
“C_Id" column in “Customer" table.
It also prevents invalid data to enter in foreign key column.
CROSS JOIN(CARTESIAN PRODUCT OR SIMPLE
JOIN)

The CROSS JOIN specifies that all rows from first


table join with all of the rows of second table.
In simple words you can say that if two tables in a join
query have no join condition, then the Oracle returns
their Cartesian product.
Image Representation of cross join

5
CROSS JOIN(CARTESIAN PRODUCT OR SIMPLE
JOIN)

Syntax:
⚫ SELECT * FROM table1 CROSS JOIN table2;
OR
SELECT * FROM table1, table2

6
SAMPLE DATA FOR QUERY

Ano Balance Bname


A01 5000 Vvn Bname Baddress
A02 6000 Ksad Vvn Mota bazaar,vvnagar
A03 7000 Anand Ksad Chhota bazaar,karamasad
A04 8000 Ksad anand Nana bazaar,anand
A05 6000 vvn

Account
Branch

7
EXAMPLE

Combine information from “account” and


“branch” table.
select * from account cross join branch;
OR
select * from account,branch;

Note: Cartesian product creates a mess by


concatenating unrelated records. So it is rarely
used. It just provides base for inner join.

8
INNER JOIN
The inner join combines only those records which
contain common values in both tables.
So in contradiction to cross join it combines only
consistent records.
It is the most frequently used join operation.
Image representation of inner join:

9
INNER JOIN SYNTAX
SELECT columns FROM table1 INNER JOIN table2
ON table1.column1 OP table2.column1;
OR
SELECT columns FROM table1,table2
WHERE table1.column1 OP table2.column2;
Here column1 is the primary key of table1 and column2 is
the foreign key.
OP is the operator
If Op is ‘=‘, then join is ‘equi join’, if compared for non
equality, then join operation is called ‘non equi join’.
10
EXAMPLE 1
Combine the consistent information from account
and branch
Query:
select
account_no,balance,account1.bname,baddress
from account1,branch where
account1.bname=branch.bname;

11
EXAMPLE 2
Find branch name and address for account
having account_number ‘A01’.

Query:
select account1.bname,baddress from
account1,branch where
account1.bname=branch.bname and
account_no='a01';

12
SELF JOIN
Self Join is a specific type of Join.
In Self Join, a table is joined with itself.
A self join simply specifies that each rows of a
table is combined with itself and every other row
of the table.

13
SELF JOIN SYNTAX
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;

Where ‘a’ and ‘b’ are alias name for table1 and
table2.

14
SAMPLE DATA

15
EXAMPLE

Display employee id and name along with name


of their manager.
Query:
select emp.eid,emp.name "Emp
name",mngr.name "Mngrname" from employee
emp,employee mngr where
emp.mngr_id=mngr.eid;

16
OUTER JOIN
In an outer join, along with tuples that satisfy
the matching criteria, we also include some or all
tuples that do not match the criteria.
Types:
⚫ Left Outer Join
⚫ Right Outer Join
⚫ Full Outer Join

17
SAMPLE DATE
Name ID Departm
ent
Manisha S01 Computer
College
Anisha S02 Computer
Nisha S03 IT

Name Hostel_name Room_no

Hostel Anisha Kaveri Hostel K01


Nisha Godavari Hostel G07
Isha Kaveri Hostel K02

18
LEFT OUTER JOIN( SYNTAX)
SELECT table1.column, table2.column
FROM table1 LEFT JOIN table2
ON (table1.column = table2.column);
OR
SELECT column1,column2,….column N
FROM table1,table2
WHERE table1.column=table2.column(+);

19
LEFT OUTER JOIN EXAMPLE
Perform left outer join on college and hostel
tables
Query:
SELECT * FROM college,hostel WHERE
college.name=hostel.name(+);
OR
SELECT college.name, department, hostel.name,
room_no
FROM college LEFT JOIN hostel
ON (college.name=hostel.name);
20
RIGHT OUTER JOIN( SYNTAX)
SELECT table1.column, table2.column
FROM table1 RIGHT JOIN table2
ON (table1.column = table2.column);
OR
SELECT column1,column2,….column N
FROM table1,table2
WHERE table1.column(+)=table2.column;

21
RIGHT OUTER JOIN EXAMPLE
Perform right outer join on college and hostel
tables
Query:
SELECT * FROM college,hostel WHERE
college.name(+)=hostel.name;
OR
SELECT college.name, department, hostel_name,
room_no
FROM college RIGHT JOIN hostel
ON (college.name=hostel.name);
22
FULL OUTER JOIN SYNTAX

SELECT table1.column, table2.column


FROM table1 FULL JOIN table2
ON (table1.column = table2.column);
OR
Left Outer Join Query UNION Right outer join
query

23
FULL OUTER JOIN EXAMPLE
Perform the full outer join operation on college
and hostel table
SELECT * FROM college,hostel WHERE
college.name=hostel.name(+)
UNION
SELECT * FROM college,hostel WHERE
college.name(+)=hostel.name;
OR
SELECT
college.name,department,hostel_name,room_no
FROM college FULL OUTER JOIN hostel
ON (college.name=hostel.name); 24
REFERENCES
Bayross I. SQL, PL/SQL: The programming
language of Oracle. BPB publications; 2009.
Book: Database management – By Bharat V
Chawda, Atul Prakashan
https://www.javatpoint.com/oracle-cross-join
https://www.javatpoint.com/oracle-inner-join
https://www.javatpoint.com/oracle-self-join

25
DISCLAIMER
The contents of the slide are taken from various
sources for academic teaching purpose only.
There is no claim for the content to be original.

26

You might also like