Sunteți pe pagina 1din 6

Appendix4-1: SQL examples

Use Branch and PropertyForRent tables in examples (a), (b) and (c).
Branch {branchNo, street, city, telNo, faxNo}
Pk
Staff {stafffNo, fNname, lName, position, sex, salary, branchNo}
pk
fk
PropertyForRent {propertyNo, street, city, type, rooms, rent, staffNo, branchNo}
Pk
fk
fk
Branch
branchNo

street

city

telNo

faxNO

B5

22 Dear Rd

London

0171-886-1212

0171-886-1214

B7

16 Argyll St

Aberdeen

01224-67125

01224-67111

B3

163 Main St

Glasgow

0141-339-2178

0141-339-4439

B4

38 Manse Rd

Bristol

0117-916-1170

0117-776-1114

B2

56 Clover Dr

London

0181-963-1030

0181-453-7992

PropertyForRent
propertyNo

street

city

type

rooms

rent

staffNo

branchNo

PA14

16 Holland

Aberdeen

House

650

SA9

B7

PL94

6 Argyll St

London

Flat

400

SL41

B5

PG4

6 Lawrence St

Glasgow

Flat

350

SG14

B3

PG36

2 Manor Rd

Glasgow

Flat

375

SG37

B3

PG21

18 Dale Rd

Glasgow

House

600

SG37

B3

PG16

5 Novar Dr

Glasgow

Flat

450

SG14

B3

I. Set Operators Examples


a) UNION Operator: Construct a list of all cities where there is either a branch office or a property
i)

ii)
(SELECT city
FROM Branch
WHERE city IS NOT NULL)
UNION
(SELECT city
FROM PropertyForRent
WHERE city IS NOT NULL);

Result
City
London
Glasgow
Aberdeen
Bristol

SELECT
*
FROM
Branch
WHERE city IS NOT NULL)
UNION CORRESPONDING BY city
(SELECT *
FROM
PropertyForRent
WHERE city IS NOT NULL);

b) INTERSECT Operator: Construct a list of all cities where there is both a branch office and a property
i)

ii)
(SELECT city
FROM Branch)
INTERSECT
(SELECT city
FROM PropertyForRent);

Result
city
London
Glasgow
Aberdeen

(SELECT *
FROM Branch)
INTERSECT CORRESPONDING BY city
(SELECT *
FROM PropertyForRent);

c) Except (MINUS in Oracle) Operator: construct a list of all cities where there is branch office but no properties.
i)

ii)

(SELECT city
FROM Branch)
EXCEPT
(SELECT city
FROM PropertyForRent);

(SELECT *
FROM branch)
EXCEPT CORRESPONDING BY city
(SELECT *
FROM PropertyForRent);

We can rewrite this query without the EXCEPT operator


i)
SELECT DISTINCT city
FROM Branch
WHERE city NOT IN (SELECT city
FROM propertyForRent);

Result
city
Bristol

ii)
SELECT city
FROM Branch b
WHERE NOT EXISTS
( SELECT *
FROM PropertyForRent p
WHERE b.city = p.city);

II. OUTER JOIN Examples


Branch1
branchNo
B003
B004
B002

bCity
Glasgow
Bristol
London

Property1
PropertyNo
PA14
PL94
PG4

pCity
Aberdeen
London
Glasgow

a) LEFT OUTER JOIN


SELECT Branch1.*, Property1.*
FROM Branch1 LEFT JOIN Property1 ON Branch1.bCity = Property1.pCity;
Result
bCity
Glasgow
Bristol
London

propertyNo
PG4
NULL
PL94

pCity
Glasgow
NULL
London

b) RIGHT OUTER JOIN


SELECT Branch1.*, Property1.*
FROM Branch1 RIGHT JOIN Property1 ON Branch1.bCity = Property1.pCity;
Result
bCity
NULL
Glasgow
London

propertyNo
PA14
PG4
PL94

pCity
Aberdeen
Glasgow
London

c) FULL OUT JOIN


SELECT Branch1.*, Property1.*
FROM Branch1 FULL JOIN Property1 ON Branch1.bCity = Property1.pCity;

Result
bCity
NULL
Glasgow
Bristol
London

propertyNo
PA14
PG4
NULL
PL94

pCity
Aberdeen
Glasgow
NULL
London

S-ar putea să vă placă și