Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Relational Algebra

Continued
Set and Join Operations
UNION
• The UNION operation of two tables is formed by appending rows from one table to those
of a second to produce a third. Duplicate rows are eliminated. Tables in an UNION
operation must have the same number of columns and corresponding columns must
come from the same domain
• The result of this operation, denoted by R U S, is a relation that includes all tuples that
are either in R or in S or in both R and S.
A Union B

k x y
k x y k x y
1 A 2
1 A 2 1 A 2
2 B 4
2 B 4 4 D 8
3 C 6
3 C 6 5 E 10
4 D 8
Table A Table B 5 E 10
• Example:
– To retrieve the social security numbers of all employees who either
work in department 5 or directly supervise an employee who works
in department 5

– We can use the union operation as follows:


DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2

– The union operation produces the tuples that are in either RESULT1
or RESULT2 or both.
– The two operands must be “type compatible”. Slide 6- 5
Relational Algebra Operations from
Set Theory: UNION (contd.)
• UNION Example

STUDENTINSTRUCTOR

Slide 6- 6
n

Slide 6- 7
Intersection

k x y Table B
1 A 2
2 B 4
Table A k x y
3 C 6 1 A 2
4 D 8
A Intersect B 5 E 10

k x y
1 A 2
Relational Algebra Operations from Set Theory: INTERSECTION

• The result of this operation, denoted by R  S, is a


relation that includes all tuples that are in both R and S.
• The two operands must be “type compatible”
– Example: The result of the intersection operation (figure
below) includes only those who are both students and
instructors.

STUDENT  INSTRUCTOR

Slide 6- 9
MINUS
The difference of two relational tables is a third that contains those
rows that occur in the first table but not in the second. The Difference
operation requires that the tables be union compatible.
The notation for difference is A MINUS B or A-B. As with arithmetic, the
order of subtraction matters. That is, A - B is not the same as B - A.

k x y k x y
1 A 2 Table A A MINUS B 2 B 4
2 B 4 3 C 6
3 C 6

k x y k x y
B MINUS A
1 A 2 4 D 8
Table B
4 D 8 5 E 10
5 E 10
Relational Algebra Operations from Set Theory

• Notice that both union and intersection are commutative operations; that is
– R  S = S  R, and R  S = S  R
• Both union and intersection can be treated as n-ary operations applicable to any
number of relations as both are associative operations; that is
– R  (S  T) = (R  S)  T
– (R  S)  T = R  (S  T)
• The minus operation is not commutative; that is, in general
– R-S≠S–R

Slide 6- 11
SQL Joins
Objectives
• Use Joins to access data from more than one table using
equality and non equality condition
• Use unconditional joins that yield a Cartesian or a cross
product
• Perform Outer and Inner joins
• Perform self joins
Data from Multiple tables
Empno Ename ….. Deptno Deptno Dname LOC
7839 King ….. 10
10 Accounting New york
7698 Blake ….. 10 20 Research Dallas
30 Sales Chicago
…..
40 Operations Boston
7934 Miller ….. 10

Data from Multiple Tables


•A need exists to retrieve Data from Multiple tables
•Empno exists in EMP table and Deptno exists in both
•Loc exists in only one table
What is a Join
• A Join involves more than one table to interact with
• Where clause specifies the Join condition
• Ambiguous columns are identified by table name
• If a data is to be retrieved from more than one table, a join condition is required
• Rows in one table can be joined to other and those which matches rows of the
Where clause is returned

SELECT TABLE1.COLUMN,TABLE2.COLUMN
FROM TABLE1,TABLE 2
WHERE TABLE1.COLUMN1=TABLE2.COLUMN2
CARTESIAN PRODUCT
• A join condition is completely omitted
• All rows in one table are joined to all rows in the second table.
• It generates a collection of rows (Spurious) which are rarely
useful.
• Emp with 14 rows, Dept with 4 rows
• EMP X DEPT = 56 rows
Product
• The product of two relational tables, also called the Cartesian Product, is the
concatenation of every row in one table with every row in the second.
• The product of table A (having m rows) and table B (having n rows) is the table
C (having m x n rows). The product is denoted as A X B or A TIMES B

ak ax ay bk bx by
k x y 1 A 2 1 A 2
1 A 2 1 A 2 4 D 8
Table A 2 B 4 1 A 2 5 E 10
3 C 6 2 B 4 1 A 2
2 B 4 4 D 8
k x y A TIMES B
2 B 4 5 E 10
1 A 2 3 C 6 1 A 2
Table B 4 D 8 3 C 6 4 D 8
5 E 10 3 C 6 5 E 10
Types of joins
• Inner Join: Retrieves the rows that matches the condition of the where
clause
– Equi Join
– Non Equi Join
– Self Join
• Outer Join: Retrieves the rows that does not match the condition of the
where clause along with the matched rows
– Left
– Right
Equi Join
• Determine all the details of an employee and Department where the
employee belongs.
• The table are connected by = operator between tables.
• Mostly Primary and Foreign keys are connected
SELECT * FROM EMP,DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO
EquiJoin
Using Table Aliases
• Table Alias can be max 30 characters
• Preferred to be shorter
• They are valid only for current select statement they are referred
• It should be meaningful for better usage and understanding of the context
• Alias name must be referred across the select statements wherever they are referred in the
statement
• Table aliases speed up database access

SELECT E.EMPNO,E.ENAME,E.DEPTNO
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO;
Natural Join
• Invariably the JOIN involves an equality test, and thus is often
described as an equi-join. Such joins result in two attributes in
the resulting relation having exactly the same value. A `natural
join' will remove the duplicate attribute(s).
Joining More than 2 Tables using Natural Join
Name Custid Custid Ordid

Itemid Ordid
• There shall be a necessity to retrieve data from more than 2 tables
• Incase id N tables are to be joined, use at a minimum N-1 conditions
SELECT C.NAME,C.CUSTID,O.ORDID,I.ITEMID
FROM CUSTOMER C,ORDER O,ITEM I
WHERE C.CUSTID = O.CUSTID AND
O.ORDID = I.ORDID;

You might also like