• Non ci sono risultati.

DBG Set operators DBG Set operatorsSet operators SQL language: basics

N/A
N/A
Protected

Academic year: 2021

Condividi "DBG Set operators DBG Set operatorsSet operators SQL language: basics"

Copied!
15
0
0

Testo completo

(1)

D B M G

SQL language: basics

Set operators Set operators

D B M G

Set operators

The UNION operator The INTERSECT operator The EXCEPT operator

(2)

D B M G

Set operators

The UNION operator The UNION operator

D B G

The UNION operator

Set union operator

It performs the union of the two relational expressions A and B

relational expressions A and B may be generated by SELECT statements

it requires schema compatibility between A and B removal of duplicates

UNION removes duplicates

UNION ALL does not remove duplicates

A UNION B

(3)

D B M G

UNION: example

P

SP

Find the codes of products that are either red or supplied by supplier S2 (or both)

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId PId Qty

S1 P1 300

S1 P2 200

S1 P3 400

S1 P4 200

S1 P5 100

S1 P6 100

S2 P1 300

S2 P2 400

S3 P2 200

S4 P3 200

S4 P4 300

S4 P5 400

D B M G

UNION: example

SELECT PId FROMP

WHEREColor=‘Red’

P

PId P1 P6

Find

the codes of products that are

either

red

or supplied by supplier S2 (or both)

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

(4)

D B M G

Find the

codes of the products that are

either red or

supplied by supplier S2

(or both)

UNION: example

SELECTPId FROMSP

WHERESId=‘S2’

SP

PId P1 P2 SId PId Qty

S1 P1 300

S1 P2 200

S1 P3 400

S1 P4 200

S1 P5 100

S1 P6 100

S2 P1 300

S2 P2 400

S3 P2 200

S4 P3 200

S4 P4 300

S4 P5 400

D B G

UNION: example

SELECT PId FROMP

WHEREColor=‘Red’

UNION SELECT PId FROMSP

WHERESId=‘S2’;

R

PId P1 P2 PId P6

P1 P2 PId

P1 P6

Find the codes of products that are either red or supplied by supplier S2 (or both)

(5)

D B M G

UNION: example

SELECT PId FROMP

WHEREColor=‘Red’

UNION SELECT PId FROMSP

WHERESId=‘S2’;

R

PId P1 P2

Duplicate P6

removal

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B M G

UNION: example

SELECT PId FROMP

WHEREColor=‘Red’

UNION SELECT PId FROMSP

WHERESId=‘S2’;

Schema compatibility

Find the codes of products that are either red or supplied by supplier S2 (or both)

(6)

D B M G

UNION ALL: example

SELECT PId FROMP

WHEREColor=‘Red’

UNION ALL SELECT PId FROMSP

WHERESId=‘S2’;

R

PId P1 P1 P2 PId P6

P1 P2 PId

P1 P6

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B G

Set operators

The INTERSECT operator

The INTERSECT operator

(7)

D B M G

Set intersection operator

It performs the intersection of the two relational expressions A and B

relational expressions A and B may be generated by SELECT statements

it requires schema compatibility between A and B

The INTERSECT operator

A INTERSECT B

D B M G

INTERSECT: example

P

S

Find the cities where both one or more suppliers and one or more stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

(8)

D B M G

INTERSECT: example

SELECT City FROMS

City London

Paris Paris London

Athens

S

Find

the cities where

both

one or more suppliers

and one or more stores

are based

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B G

INTERSECT: example

SELECT Store FROMP

P

Store London

Paris Rome London

Paris London

Find

the cities where

both one or more suppliers and

one or more stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

(9)

D B M G

Find the cities where both one or more suppliers and one or more stores are based

INTERSECT: example

SELECT City FROMS INTERSECT SELECT Store FROMP;

London Paris

R

City London

Paris Paris London Athens

Store London

Paris Rome London

Paris London

D B M G

Equivalence with other operators

The intersection operation may also be performed by means of

a join

the INoperator

(10)

D B M G

Equivalence with join

The FROM clause contains the relations involved in the intersection

The WHERE clause contains join conditions between the attributes listed in the SELECT clauses of relational expressions A and B

D B G

Equivalence with join: example

SELECT City FROMS, P

WHERE S.City=P.Store;

Find the cities where both one or more suppliers and one or more stores are based

(11)

D B M G

Equivalence with the IN operator

One of the two relational expressions is turned into a nested query using operator IN

The attributes in the outer SELECT clause, joined together by a tuple constructor, make up the left- hand side of the IN operator

D B M G

Equivalence with IN: example

SELECT Store FROMP

WHEREStore IN(SELECT City FROMS);

Find the cities where both one or more suppliers and one or more stores are based

(12)

D B M G

Set operators

The EXCEPT operator The EXCEPT operator

D B G

The EXCEPT operator

A EXCEPT B Set difference operator

It subtracts relational expression B from relational expression A

it requires schema compatibility between A and B

(13)

D B M G

EXCEPT: example

P

S

Find the cities where one or more suppliers, but no stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B M G

EXCEPT: example

SELECT City FROMS

City London

Paris Paris London

Athens

S

Find

the cities where one or more suppliers

, but no stores

are based

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

(14)

D B M G

EXCEPT: example

SELECT Store FROMP

P

Store London

Paris Rome London

Paris London

Find

the cities where

one or more suppliers, but no

stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

D B G

EXCEPT: example

SELECT City FROMS EXCEPT SELECT Store FROMP;

Athens

R

City London

Paris Paris London Athens

Store London

Paris Rome London

Paris

Find the cities where one or more suppliers, but no stores are based

(15)

D B M G

Equivalence with the NOT IN operator

The difference operation may also be performed by means of the NOT IN operator

relational expression B is nested within the NOT IN operator

the attributes in the SELECT clause of relational expression A, joined together by a tuple

constructor, make up the left-hand side of the NOT IN operator

D B M G

Equivalence with the NOT IN operator: example

SELECT City FROMS

WHERECity NOT IN (SELECTStore FROMP);

Find the cities where one or more suppliers, but no stores are based

Riferimenti

Documenti correlati

CREATE VIEW SMALL_SUPPLIERS AS SELECT SId, SName, #Employees, City FROM S.

The database is in a new (final) correct state The changes to the data executed by the transaction

it is sufficient to specify in which host language variable the result of the command shall be stored If an SQL command returns a table (i.e., a set of tuples). a method is

Returns the array corresponding to the current row, or FALSE in case there are no rows available Each column of the result is stored in an element of the array, starting from

CUSTOMER (CustCode, CustName, Gender, AgeRange, CustCountry) VACATION-RESORT (ResCode, ResName, ResType, Location, ResCountry). RESERVATION(CustCode, StartDate,

D102 Smith Computer engineering D105 Jones Computer engineering D104 White Electronics. ProfID PSurname

Per ogni anno di iscrizione, trovare la media massima (conseguita da uno studente). D B

Informazioni sulle tabelle Il dizionario dei dati contiene per ogni tabella della base di dati. nome della tabella e struttura fisica del file in cui è