24
Set Operations Objectives of the Lecture : •To consider the Set Operations Union, Difference and Intersect; •To consider how to use the Set Operations in SQL.

Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Embed Size (px)

Citation preview

Page 1: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Set Operations

Objectives of the Lecture :

•To consider the Set Operations Union, Difference and Intersect;

•To consider how to use the Set Operations in SQL.

Page 2: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Union (1)

A Union B

A

B

Page 3: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Union (2)

A Union B

A

B

Page 4: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Definition of Union

Given A Union B

The result is a relation that contains a copy of all the tuples that are in A, and a copy of all the tuples that are in B.

If a tuple appears in both A and B, then only one copy of it appears in the result. (The result cannot contain duplicate tuples).

B Union Ai.e. the result is the same regardless of which way

round the operands are. So Union is said to be commutative.

Page 5: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Difference (1)

A

B

A Difference B

Page 6: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Difference (2)

A

B

A Difference B

Page 7: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Definition of Difference

Given A Difference B

Result is a relation that contains all the tuples of A which do not also appear in B.

B Difference Ai.e. it does matter which way round the

operands are, because it affects the result.So Difference in not commutative - it’s like

subtraction in arithmetic)

Page 8: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Intersect (1)

A Intersect B

A

B

Page 9: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Relational Intersect (2)

A

B

A Intersect B

Page 10: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Definition of Intersect

Given A Intersect B

Result is a relation that contains all the tuples that appear in both A and B,

but only one copy of each tuple to avoid duplicates).

B Intersect Ai.e. the result is the same regardless of which way

round the operands are. So Intersect is also commutative.

Page 11: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Operand Type Compatibility

The 2 operands of a set operator must : have the same number of attributes; each corresponding attribute must have the same

domain / type; corresponding attributes are recognised by having the

same name.

Otherwise the tuples of one relation cannot be compared with the tuples of the other. the operator cannot work.

This is traditionally known as Union Compatibility, but as it applies to to all 3 operators, so it would be better to call it Type Compatibility.

Page 12: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Result of a Set Operation The result has the same number of attributes as the

operands.

Each result attribute has the same data type as the corresponding attributes in the operands.

Each result attribute has the same name as the corresponding attributes in the operands.

The number of tuples in the result depends on the operator used.

Note that Set Operators merge two relations into one.They provide an alternative strategy for merging

relations to the Join Operators.Set Operators ‘merge tuples’, whereas Join

Operators ‘merge attributes’

Page 13: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

SQL : Set Operators

SQL does not express set operations within a SELECT statement.

It uses algebra-like operators : UNION EXCEPT ( Difference) INTERSECT

The operands of an SQL set operator are provided by 2 SELECT statements.

Some relational DBMSs- e.g. Oracle -

use the keywordMINUS

instead ofEXCEPT.

Page 14: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Example of SQL Union

SELECT S#, P#FROM ORDERS

UNION

SELECT S#, P#FROM SHIPMENTS ;

Explanation : Get all the part numbers and supplier numbers of all parts that are on order or being shipped.

No semicolon.

A semicolon.

Page 15: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Example of SQL Difference

SELECT S#, P#FROM ORDERS

EXCEPT

SELECT S#, P#FROM SHIPMENTS ;

Explanation : Get all the part numbers and supplier numbers of all parts that are on order but not being shipped.

MINUS in Oracle.

Page 16: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Example of SQL Intersect

SELECT S#, P#FROM ORDERS

INTERSECT

SELECT S#, P#FROM SHIPMENTS ;

Explanation : Get all the part numbers and supplier numbers of all parts that are on order and also being shipped.

Page 17: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

The Complexity of SQL Set Operands

Each SELECT statement ‘operand’ can be as large and as complex as desired.

Example :-SELECT S#, P#FROM ORDERS NATURAL JOIN STOCKWHERE S# = ‘S1’ AND O-QTY > 200

INTERSECT

SELECT S#, P#FROM SHIPMENTSWHERE S# = ‘S1’ AND Sh-QTY > 200 ;

Page 18: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

SQL Operand Type Compatibility

Corresponding columnsare not determined by column namebut by column position in the SELECT phrases.

Thus 1st column name in first SELECT phrase goes with 1st column name in the second SELECT phrase, 2nd column name in first SELECT phrase goes with 2nd column name in the second SELECT phrase,

and so on for all column.

Corresponding columns can have different names.

Corresponding columns must have the same type, or allow the DBMS to cast from one type to anothere.g. may cast from CHAR(2) to CHAR(3), but not from CHAR(3) to CHAR(2).

Page 19: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Result of an SQL Set Operation

The result’s column names are those appearing in the first SELECT phrase, if the names differ between the SELECT phrases).

If the SQL tables contain duplicate rows, all 3 operators remove duplicate rows from the result: i.e. they work as proper set operators.

Note that no SELECT phrases may include the DISTINCT keyword. It is not necessary anyway, since duplicate rows are removed.

Page 20: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Usage of UnionSELECT *FROM EMPWHERE Sal = 20000

UNIONSELECT *FROM EMPWHERE M-S = ‘S’ ;

is the same as

SELECT *FROM EMPWHERE Sal = 20000 OR M-S = ‘S’ ;

it is rarely useful to do a Union of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Page 21: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Usage of DifferenceSELECT *FROM EMPWHERE Sal = 20000

EXCEPT SELECT *FROM EMPWHERE M-S = ‘S’ ;

is the same as

SELECT *FROM EMPWHERE Sal = 20000 AND NOT M-S = ‘S’ ;

it is rarely useful to do a Difference of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Page 22: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Usage of Intersect

SELECT *FROM EMPWHERE Sal = 20000

INTERSECT SELECT *FROM EMPWHERE M-S = ‘S’ ;

is the same as

SELECT *FROM EMPWHERE Sal = 20000 AND M-S = ‘S’ ;

it is rarely useful to do an Intersect of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Page 23: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Usage of Set Operators in General

Set operators are rarely used when their operands come from the same relation, because it is usually simpler to use a Restrict condition in the WHERE phrase of a single SQL statement.

Set operators can be very useful when the operands come from different relations, but they are not often used in this way, because a consequence of the type incompatibilites of the different tables in a DB is that there are limited useful opportunities for such operations.

in practice Join operations are much more frequently used for ‘merging relations’ than are Set operations.

Page 24: Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in

Using Multiple Set Operators in SQL

Example :-

SELECT …..FROM ….. ………..

UNION

SELECT …..FROM ….. ………..

INTERSECT

SELECT …..FROM ….. ………..

etc

Sequenceof

execution