0% found this document useful (0 votes)
16 views17 pages

14-MySQL Advance Cs 12

Uploaded by

studyornomoney7
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)
16 views17 pages

14-MySQL Advance Cs 12

Uploaded by

studyornomoney7
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/ 17

Revised as

Computer Science per


CBSE
Curriculum
Class XII (CBSE Board) 2020-21

UNIT-3: Database Management


Chapter:14
MySQL- Advanced

Visit www.ip4you.blogspot.com for more….

Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)


Kendriya Vidyalaya Khanapara, Guwahati (Assam)
e-mail : [email protected]
Expected Learning Outcome
In this presentation you will learn about
handling query which involves access of
two tables.
 Understanding JOIN Query
 Concept of JOIN Operation.
 Types of JOIN
 Implementing JOIN operation in MySQL
 Implementing UNION operations in MySQL
Displaying Data from Multiple Tables - Join Query

Some times it is required to access the


information from two or more tables, which
requires the Joining of two or more tables.
Such query is called Join Query.
MySQL facilitates you to handle Join Queries
in different ways.
The major types of Join is as follows-
 Cross Join (Cartesian Product)
 Equi Join
 Non-Equi Join
 Natural Join
Concept of Join Operation
Suppose you are having three shirts (S1,S2,S3) and two pants
(P1 and P2).
How
many
types
you can
wear ?

3 Shirts
2 Pants

3x2 =6
i.e. 6
types are
possible
Cross Join – Mathematical Principle
Consider the two set A= {a,b} and B={1,2}
The Cartesian Product i.e. A x B = {(a,1) (a,2) (b,1) (b,2)}
Similarly, we may compute Cross Join of two tables by joining
each Record of first table with each record of second table.
RxS
R S
A B C C X Y
A B C C X Y
p q s s q r
p q s s q r
X p q s t n m
m n t t n m
p q s o p s
o p s o p s
m n t s q r
l m u
m n t t n m
The table will contain m n t o p s
(4x3=12) rows and 6 columns.
… … … … .. ..
So, degree will be added and
cardinality is multiplied for l m u o p s
Result table
Equi Join – Mathematical Principle
In Equvi Join, records are joined on the equality condition of
Joining Column. Generally, the Join column is a column which is
common in both tables (Foreign Key).
Consider the following table R and S having C as Join column.

R S T (Equi Join)
A B C C X Y
A B C C X Y
p q s s q r
p q s s q r
m n t t n m
m n t t n m
o p s o p s
o p s s p r
l m u

The result table will contain 6 columns but


records are selected those are having Equal
value for C column in both table.
Non-Equi Join – Mathematical Principle
In Non-Equi Join, records are joined on the condition other than
Equal operator (>,<,<>,>=,<=) for Joining Column (common
column).
Consider the following table R and S having C as Join column and
<> (not equal) operator is applied in join condition.
R S T (Non-Equi Join)
A B C C X Y A B C C X Y
<>
p q s s q r p q s t n m
p q s o p s
m n t t n m
m n t s p r
o p s o p s
m n t o p s
l m u
o p s t n m
o p s o p s
The result table will contain 6
l m u s q R
columns but records are selected
those are having not- equal value l m u t n M
for C column in both table. l m u o p s
Natural Join – Mathematical Principle
The Natural Join is much similar to Equi Join i.e. records are
joined on the equality condition of Joining Column except that the
common column appears one time.
Consider the following table R and S having C as Join column.

R S T (Natural Join)
A B C C X Y
A B C X Y
p q s s q r
p q s q r
m n t t n m
m n t n m
o p s o p s
o p s p r
l m u

The result table will contain 5 columns (common column is


eliminated ) but records are selected those are having
Equal value for C column in both table.
Implementing Join Operation in MySQL

Consider the two tables EMP and DEPT - Foreign Key

Primary Key EmpID EName City Job Pay DeptNo


E1 Amitabh Mumbai Manager 50000 D1
E2 Sharukh Delhi Manager 40000 D2
EMP E3 Amir Mumbai Engineer 30000 D1
E4 Kimmi Kanpur Operator 10000 D2
E4 Puneet Chennai Executive 18000 D3
E5 Anupam Kolkatta Manager 35000 D3

DEPT E6 Syna Banglore Secretary 15000 D1


… …. …. …. … …

DeptNo DName Location


Suppose we want complete
Primary Key

D1 Production Mumbai details of employees with their


D2 Sales Delhi Deptt. Name and Location……
D3 Admn Mumbai
this query requires the join of
both tables
D4 Research Chennai
How to Join ?
MySQL offers different ways by which you may join two or more tables.
 Method 1 : Using Multiple table with FROM clause and
JOIN condition.
The simplest way to implement JOIN operation, is the use of
multiple table with FROM clause followed with Joining
condition in WHERE clause.
To avoid ambiguity
you should use
Select * From EMP, DEPT Qualified name i.e.
Where Emp.DeptNo = Dept.DeptNo ; <Table>.<column>

If common column are differently spelled then no need to use


Qualified name.
 Method 2: Using JOIN keyword
MySQL offers JOIN keyword, which can be used to implement
all type of Join operation.
Select * From EMP JOIN DEPT ON Emp.DeptNo=Dept.DeptNo ;
Method 1-Using Multiple Table with FROM clause

The General Syntax of Joining table is-


SELECT < List of Columns> FROM <Table1, Table 2, …>
WHERE <Joining Condition> [Order By ..] [Group By ..]
 You may add more conditions using AND/OR/NOT operators,
if required.
 All types of Join (Equi, No-Equi, Natural etc. are implemented
by changing the Operators in Joining Condition and selection
of columns with SELECT clause.
Ex. Find out the name of Employees working in Production Deptt.
Select Ename From EMP, DEPT
Where Emp.DeptNo=Dept.DeptNo AND Dname=‘Production’;
Ex. Find out the name of Employees working in same city from where
they belongs (hometown).
Select Ename From EMP, DEPT
Where Emp.DeptNo=Dept.DeptNo And City=Location;
Method-2:Using JOIN keyword with FROM clause

MySQL offers the following keywords to implement various types on Join


operations. These Keyword may be used with From clause.
 JOIN … ON <Condition> #Joining tables based on condition
 CROSS JOIN # Implements Cartesian product
 NATURAL JOIN # Implements Natural Join

SELECT < List of Columns>


FROM <Table1> JOIN <Table2> ON <Joining Condition>

SELECT < List of Columns>


FROM <Table1> JOIN <Table2>
OR
SELECT < List of Columns>
FROM <Table1> CROSS JOIN <Table2>

SELECT < List of Columns>


FROM <Table1> NATURAL JOIN <Table2>
Examples of JOIN Query

All above
quarries give
same result
Example of Join Query

Ex. Find out the name of Employees working in Production Deptt.


Select Ename From EMP, DEPT
Where Emp.DeptNo=Dept.DeptNo and Dname=‘Production’;
OR
Select Ename From EMP JOIN DEPT ON Emp.DeptNo=Dept.DeptNo
Where Dname=‘Production’;

Ex. Find out the name of Employees working in same city from where
they belongs (hometown) .
Select Ename From EMP,DEPT Where Emp.City = Dept.Location;
OR
Select Ename From EMP JOIN DEPT ON Emp.City = Dept.Location;

Ex. Find out the all details of Employees with their department details.
Select * From EMP,DEPT Where Emp.DeptNo=Dept.DeptNo;
OR
Select * FROM EMP NATURAL JOIN DEPT;
Nested Query (A query within another query)
Sometimes it is required to join two sub-queries to solve a
problem related to the single or multiple table. Nested query is
the form of query which contains multiple query and inner query
evaluated first.
The general form to write Nested query is-
Select …. From <Table> Where <Column1> <Operator>
(Select Column1 From <Table> [Where <Condition>])

Ex. Find out the name of Employees working in Production Deptt.


Select Ename From EMP
Where DeptNo = (Select DeptNo From DEPT Where
DName=‘Production’);
Ex. Find out the name of Employees who are getting more pay than
‘Ankit’.
Select Ename From EMP
Where Pay >= (Select Pay From EMP Where Ename=‘Ankit’ );
Union of Tables
Sometimes it is required to combine all records of two tables
without having duplicate records. The combining records of two
tables is called UNION of tables.
UNION Operation is similar to UNION of Set Theory.
E.g. If set A= {a,c,m,p,q} and Set B= {b,m,q,t,s}
Then AUB= {a,c,m,p,q,b,t,s}
[All members of Set A and Set B are taken without repeating]
Select …. From <Table1>[Where <Condition>]
UNION [ALL]
Select …. From <Table2> [Where <Condition>];
Ex. Select Ename From PROJECT1
UNION
Select Ename From PROJECT2 ;
Both tables or output of queries must be UNION compatible i.e. they
must be same in column structure (number of columns and data
types must be same).
Summery..
 Queries involves two or more tables are called JOIN Queries.
 Cartesian Product (Cross Join) combines each record of first
table with each record of second table to compute all possible
combinations of records from both tables.
 Equvi join combines table on equality condition for given
columns.
 Natural Join combines tables on common column on equal
condition. It displays duplicate columns once.
 MySQL offers different ways to implement JOIN Queries
through Joining conditions with WHERE clause or using
keywords like CROSS JOIN, NATURAL JOIN etc.
 Nested Query is a form of Query in which two queries are
connected through an operator and inner query executes
first.
 UNION is an operation which combines two similar structured
tables without repeating records.

You might also like