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

Lecture2

Union relational oprator

Uploaded by

zahidullahh72
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture2

Union relational oprator

Uploaded by

zahidullahh72
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

RAO from Set theory:

1-Union Relational Operation:


 Union is symbolized by U, and includes all tuples
that are in R or in S, eliminating duplicate tuples,
therefore set R union set S would be expressed as:
 Result RuS
 Example of Union

Rules:
1- No of Col & Order
should be same
2- DTs should be
same
By Habibi 1
Example- Union

By Habibi 2
Union Operator Implementation
 Set operators are used to combine the result of 2 or
more select statements as a single unit.

 They cannot apply on insert, delete, and update


commands.

 SQL Server supports these set operators: Union,


Union All, Intersect, and Except.

 Before implementing the set operators we should


follow the basic Rules those are:
By Habibi 3
Cont.….
1.Number of columns and order of the columns should be
same within both tables.
2.Those columns data types should be comparable (well
matched), for example char with char, varchar with
varchar and so on.
 Create table EMP_KBL (EID INT, ENAME
VARCHAR (20), SALARY MONEY)
 Create table EMP_Nan (EID INT, ENAME
VARCHAR (30), SALARY MONEY)
By Habibi 4
Cont.….
Output tables will be:
EID ENAME SALARY EID ENAME SALARY
101 Ahmad 85000 101 Watan 85000
102 Kamal 45000
105 Mujeeb 72000
102 Waheed 15000
104 Aslam 33000 106 Tasal 10000

EMP_KBL EMP_NAN

By Habibi 5
Cont.….
1-Union: - it returns all values without duplicate values
from multiple tables.
 Example: - write a query to display all employee details
of an organization.
 Select * from Emp_Kab union Select * from EMP_Nan
2-Union All: - it returns all values including duplicate
values.
 Select * from Emp_Kab union all Select * from
EMP_Nan
By Habibi 6
RAO from Set Theory: Cont….
2-Intersection Relational Operation:
 Intersection is symbolized by , and includes all
tuples that only in R and S.
 Intersection operation on a relation A intersect to
relation B, therefore set A intersect to set B would be
expressed as:
 Result R S
 Example

By Habibi 7
Example-Intersection

By Habibi 8
Intersect Operator Implementation

Intersect: - it returns the common values from


multiple tables.

 Example: write a query to display employee details


who are working in both branches

 Select * from EMP_Kab intersect Select * from


EMP_Nan

By Habibi 9
RAO from Set Theory: Cont….
3- Set Difference Relational Operation:
 Set difference operation includes tuples from one
relation that not in another relation and symbolized
by the – (Minus) symbol.
 Therefore R-S would be expressed as:
 Result R-S
 Example

By Habibi 10
Example-Set Difference

By Habibi 11
Set Difference Operator Implementation
4-Except: -it returns all values from left hand side
table which are not founded in right hand side.
 Example: write a query to display employee names
who are working in Kabul branch but not in
Nangarhar branch
 Select * from EMP_Kab except select * from
EMP_Nan
 Note: Set operators are retrieving the data from
multiple tables with row wise.
By Habibi 12
Cartesian Product
 Creates a relation that has all the attributes of R and
S, allowing all the possible combinations of tuples
from R and S in the result.
 The notation used in Cartesian Product is X.
 C=RxS RxS

By Habibi 13
Example- CP

By Habibi 14
Cartesian Product Implementation

By Habibi 15
Binary RAO

 Joins is used to retrieving data from multiple tables.

 The Join operation is denoted by the R1x1S symbol


and is used to join similar tuples from two relations
into single longer relation.

 The notation used is: R join join condition S

 Here we are discussing Outer Join

By Habibi 16
Example of Join Binary Operation

By Habibi 17
Outer Join
 There are three forms of the outer join.

1. Left outer join: - keep data from the left hand


table:

2. Right outer join: - keep data from the right


hand table:

3. Full outer join: - keep data from both tables:

By Habibi 18
Outer Join Implementation
1- Create table student (ID INT, NAME VARCHAR
(20), cid int)

 insert into student values ( 101, 'Ali', 1),(102, 'Khan',


3),(103, 'Jan', 5), (104, 'Wasil',6)

2- Create table course (cid INT, C_Name VARCHAR


(20), fee MONEY)

 insert into course values( 1, 'C++', 1000),(2, 'DB',


1200), (3, 'DBA', 1300), (4, 'Java', 1400)
By Habibi 19
Cont.….

1- select * from student s left outer join course c on

s.cid=c.cid // Left Outer Join

2- select * from student s right outer join course c on

s.cid=c.cid // Right Outer Join

3- select * from student s full outer join course c on

s.cid=c.cid // Full Outer Join


By Habibi 20
Aggregate Function
 Aggregate Functions are used to calculate the result
of group values.
 Aggregate Functions are: SUM, MIN, MAX,
COUMT, AVG

By Habibi 21
Aggregate Function------Sum

By Habibi 22
Aggregate Function-----Count

By Habibi 23
Aggregate Function-----Min/AVG

By Habibi 24
Any Questions?

By Habibi 25

You might also like