Problems: 6.32, 8.11.32
Query
Get COLORs of parts supplied by S1.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| S2 | Jones | 10 | Paris | S1 | P1 | J4 | 700 | |||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | 12 | London | S2 | P3 | J6 | 400 | |||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | J3 | 300 | ||
| S4 | P6 | J7 | 300 | |||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| J2 | Display | Rome | S5 | P5 | J5 | 500 | ||||
| J3 | OCR | Athens | S5 | P5 | J7 | 100 | ||||
| J4 | Console | Athens | S5 | P6 | J2 | 200 | ||||
| J5 | RAID | London | S5 | P1 | J4 | 100 | ||||
| J6 | EDS | Oslo | S5 | P3 | J4 | 200 | ||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | J4 | 500 |
Example Query: 6.32, 8.11.32 (Cont.) [131]
Query
Get COLORs of parts supplied by S1.
Algebra
QBE
SPJ S# P# J# QTY "S1" _ ![]()
P P# PNAME COLOR WEIGHT CITY _ .P
Shell SQL
sql_restrict SPJ where 'S# == "S1"' >temp.tbl
sql_join temp P P# P# >temp1.tbl
sql_project temp1 COLOR >temp2.tbl
sql_unique temp2
Select
sql_select unique P.COLOR from P SPJ
where '(P.P# == SPJ.P#)&&(SPJ.S# == "S1")'
ANSI SQL
select distinct COLOR from P, SPJ
where P.P_NO = SPJ.P_NO and SPJ.S_NO = "S1"
RESULT COLOR Red
Example Query: 6.33, 8.11.33 [132]
Query
Get P# supplied to any project in LONDON.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| S2 | Jones | 10 | Paris | S1 | P1 | J4 | 700 | |||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | J5 | 600 | ||
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | J7 | 800 | |||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | J3 | 300 | ||
| S4 | J7 | 300 | ||||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| J2 | Display | Rome | S5 | J5 | 500 | |||||
| J3 | OCR | Athens | S5 | J7 | 100 | |||||
| J4 | Console | Athens | S5 | P6 | J2 | 200 | ||||
| J5 | RAID | London | S5 | P1 | J4 | 100 | ||||
| J6 | EDS | Oslo | S5 | P3 | J4 | 200 | ||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | J4 | 500 |
Example Query: 6.33, 8.11.33 (Cont.) [133]
Query
Get P# supplied to any project in LONDON.
Algebra
QBE
SPJ S# P# J# QTY .P _ ![]()
J J# JNAME CITY _ ![]()
"London"
Shell SQL
sql_restrict J where '(CITY=="London")' >temp.tbl
sql_join temp SPJ J# J# >temp1.tbl
sql_project temp1 P# >temp2.tbl
sql_unique temp2
Select
sql_select unique SPJ.P# from J SPJ
where '(J.J# == SPJ.J#)&&(J.CITY=="London")'
ANSI SQL
select distinct P_NO from J, SPJ
where J.J_NO = SPJ.J_NO and CITY = "London"
RESULT P# P3 P5 P6
Example Query: 6.34, 8.11.34 [134]
Query
Get J# for projects using at least one part AVAILABLE from supplier S4.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
|---|---|---|---|---|---|---|---|---|---|---|
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| S2 | Jones | 10 | Paris | S1 | P1 | J4 | 700 | |||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | 300 | |||
| S4 | P6 | 300 | ||||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| J2 | Display | Rome | S5 | P5 | J5 | 500 | ||||
| J3 | OCR | Athens | S5 | P5 | J7 | 100 | ||||
| J4 | Console | Athens | S5 | P6 | 200 | |||||
| J5 | RAID | London | S5 | P1 | J4 | 100 | ||||
| J6 | EDS | Oslo | S5 | P3 | J4 | 200 | ||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | 500 |
Example Query: 6.34, 8.11.34 (Cont.) [135]
Query
Get J# for projects using at least one part AVAILABLE from supplier S4.
Algebra
QBE
SPJ S# P# J# QTY "S4" _ ![]()
SPJ S# P# J# QTY _ ![]()
.P
Shell SQL
sql_restrict SPJ where '(S# == "S4")' >temp.tbl
sql_project temp P# >temp1.tbl
sql_join temp1 SPJ P# P# >temp2.tbl
sql_project temp2 J# >temp3.tbl
sql_unique temp3
Select
sql_copy SPJ SPJX; sql_copy SPJ SPJY
sql_select unique SPJY.J# from SPJX SPJY
where '(SPJX.P# == SPJY.P#)&&(SPJX.S# == "S4")' ???
RESULT J# J2 J3 J4 J7
ANSI SQL: 6.34, 8.11.34 (Cont.) [136]
select distinct SPJX.J_NO from SPJ as SPJX, SPJ as SPJY where SPJX.P_NO = SPJY.P_NO and (SPJX.S_NO = "S4" or SPJY.S_NO = "S4")
Example Query: 6.35, 8.11.35 [137]
Query
Get P# supplied by suppliers who also supply green parts.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
|---|---|---|---|---|---|---|---|---|---|---|
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| S2 | Jones | 10 | Paris | S1 | P1 | J4 | 700 | |||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | J3 | 300 | ||
| S4 | P6 | J7 | 300 | |||||||
| (J) | J# | JNAME | CITY | S5 | J2 | 200 | ||||
| J1 | Sorter | Paris | S5 | J4 | 100 | |||||
| J2 | Display | Rome | S5 | J5 | 500 | |||||
| J3 | OCR | Athens | S5 | J7 | 100 | |||||
| J4 | Console | Athens | S5 | J2 | 200 | |||||
| J5 | RAID | London | S5 | J4 | 100 | |||||
| J6 | EDS | Oslo | S5 | J4 | 200 | |||||
| J7 | Tape | London | S5 | J4 | 800 | |||||
| S5 | J4 | 400 | ||||||||
| S5 | J4 | 500 |
Example Query: 6.35, 8.11.35 (Cont.) [138]
Query
Get P# supplied by suppliers who also supply green parts.
Algebra
QBE
SPJ S# P# J# QTY _ ![]()
_ ![]()
SPJ S# P# J# QTY _ ![]()
.P
P P# PNAME COLOR WEIGHT CITY _ "Green"
Shell SQL
sql_restrict P where 'COLOR=="Green"' >temp.tbl
sql_join temp SPJ P# P# >temp1.tbl
sql_project temp1 S# >temp2.tbl
sql_join temp2 SPJ S# S# >temp3.tbl
sql_project temp3 P# >temp4.tbl; sql_unique temp4
Select
sql_select unique SPJ.S# from SPJ P
where '(SPJ.P#==P.P#)&&(P.COLOR=="Green")' >temp.tbl
sql_rename temp SPJ.S# S# >t.tbl
sql_select unique SPJ.P# from SPJ t where '(SPJ.S#==t.S#)'
RESULT P# P1 P2 P3 P4 P5 P6
ANSI SQL: 6.35, 8.11.35 (Cont.) [139]
select distinct SPJY.P_NO from P, SPJ as SPJX, SPJ as SPJY where SPJX.P_NO = P.P_NO and P.COLOR = "Green" and SPJY.S_NO = SPJX.S_NO
Example Query: 6.36, 8.11.36 [140]
Query
Get S# for suppliers with a status lower than S1.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
|---|---|---|---|---|---|---|---|---|---|---|
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| Jones | 10 | Paris | S1 | P1 | J4 | 700 | ||||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | J3 | 300 | ||
| S4 | P6 | J7 | 300 | |||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| J2 | Display | Rome | S5 | P5 | J5 | 500 | ||||
| J3 | OCR | Athens | S5 | P5 | J7 | 100 | ||||
| J4 | Console | Athens | S5 | P6 | J2 | 200 | ||||
| J5 | RAID | London | S5 | P1 | J4 | 100 | ||||
| J6 | EDS | Oslo | S5 | P3 | J4 | 200 | ||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | J4 | 500 |
Example Query: 6.36, 8.11.36 (Cont.) [141]
Query
Get S# for suppliers with a status lower than S1.
Algebra
Shell SQL
sql_restrict S where '(S# == "S1")' >temp.tbl
sql_project temp STATUS >temp1.tbl
sql_rename temp1 STATUS STAT1 >temp2.tbl
sql_times S temp2 >temp3.tbl
sql_restrict temp3 where '(STATUS < STAT1)' >temp4.tbl
sql_project temp4 S#
Select
sql_copy S SX; sql_copy S SY
sql_select SX.S# from SX SY
where '(SX.STATUS < SY.STATUS)&&(SY.S# == "S1")'
ANSI SQL
select SX.S_NO from S as SX, S as SY
where SX.STATUS < SY.STATUS and SY.S_NO = "S1"
RESULT S# S2
Example Query: 6.37 [142]
Query
Get J# whose CITY is FIRST alphabetically.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
| S1 | Smith | 20 | London | S1 | P1 | J1 | 200 | |||
| S2 | Jones | 10 | Paris | S1 | P1 | J4 | 700 | |||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | J3 | 300 | ||
| S4 | P6 | J7 | 300 | |||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| J2 | Display | Rome | S5 | P5 | J5 | 500 | ||||
| OCR | Athens | S5 | P5 | J7 | 100 | |||||
| Console | Athens | S5 | P6 | J2 | 200 | |||||
| J5 | RAID | London | S5 | P1 | J4 | 100 | ||||
| J6 | EDS | Oslo | S5 | P3 | J4 | 200 | ||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | J4 | 500 |
Example Query: 6.37 (Cont.) [143]
Query
Get J# whose CITY is FIRST alphabetically.
Algebra
Shell SQL
sql_summarize J CITY >temp.tbl
sql_project temp MIN >temp1.tbl
sql_join J temp1 CITY MIN >temp2.tbl
sql_project temp2 J#
ANSI SQL
select J_NO from J
where CITY = ( select min (CITY) from J )
RESULT J# J3 J4
Example Query: 6.37a MINUS (Cont.) [144]
Algebra
Shell SQL
sql_rename J CITY XCITY >temp.tbl
sql_rename J CITY YCITY >temp1.tbl
sql_times temp temp1 >temp2.tbl
sql_restrict temp2 where 'XCITY > YCITY' >temp3.tbl
sql_project temp3 XCITY >temp4.tbl
sql_unique temp4 >temp5.tbl
sql_project J CITY >temp6.tbl
sql_unique temp6 >temp7.tbl
sql_minus temp7 temp5 >temp8.tbl
sql_join temp8 J CITY CITY >temp9.tbl
sql_project temp9 J#
select J_NO
from J
where J_NO not in
( select distinct J1.J_NO /* has a city which is greater */
from J as J1, J as J2 /* cross product */
where J1.CITY > J2.CITY
)
Example Query: 6.40 [145]
Query
Get J# for projects NOT supplied with any RED part by any LONDON supplier.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
|---|---|---|---|---|---|---|---|---|---|---|
| S1 | Smith | 20 | London | S1 | P1 | 200 | ||||
| S2 | Jones | 10 | Paris | S1 | P1 | 700 | ||||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |
| P1 | Nut | Red | 12 | London | S2 | P3 | J6 | 400 | ||
| P2 | Bolt | Green | 17 | Paris | S2 | P3 | J7 | 800 | ||
| P3 | Screw | Blue | 17 | Rome | S2 | P5 | J2 | 100 | ||
| P4 | Screw | Red | 14 | London | S3 | P3 | J1 | 200 | ||
| P5 | Cam | Blue | 12 | Paris | S3 | P4 | J2 | 500 | ||
| P6 | Cog | Red | 19 | London | S4 | P6 | 300 | |||
| S4 | P6 | 300 | ||||||||
| (J) | J# | JNAME | CITY | S5 | P2 | J2 | 200 | |||
| J1 | Sorter | Paris | S5 | P2 | J4 | 100 | ||||
| Display | Rome | S5 | P5 | J5 | 500 | |||||
| J3 | OCR | Athens | S5 | P5 | J7 | 100 | ||||
| J4 | Console | Athens | S5 | P6 | J2 | 200 | ||||
| RAID | London | S5 | P1 | J4 | 100 | |||||
| EDS | Oslo | S5 | P3 | J4 | 200 | |||||
| J7 | Tape | London | S5 | P4 | J4 | 800 | ||||
| S5 | P5 | J4 | 400 | |||||||
| S5 | P6 | J4 | 500 |
Example Query: 6.40 (Cont.) [146]
Query
Get J# for projects NOT supplied with any RED part by any LONDON supplier.
Algebra
Shell SQL
sql_restrict P where 'COLOR=="Red"' >temp.tbl
sql_restrict S where 'CITY=="London"' >temp1.tbl
sql_join temp SPJ P# P# >temp2.tbl
sql_join temp2 temp1 S# S# >temp3.tbl
sql_project temp3 J# >temp4.tbl
sql_unique temp4 >temp5.tbl
sql_project J J# >temp6.tbl
sql_minus temp6 temp5
RESULT J# J2 J5 J6
ANSI SQL: 6.40 (Cont.) [147]
select J_NO
from J
where J_NO not in
( select J.J_NO
from SPJ, P, S
where SPJ.J_NO = J.J_NO
and SPJ.P_NO = P.P_NO
and SPJ.S_NO = S.S_NO
and P.COLOR = "Red"
and S.CITY = "London"
)
Example Query: 6.41 [148]
Query
Get J# for projects supplied entirely by S2.
| (S) | S# | SNAME | STATUS | CITY | (SPJ) | S# | P# | J# | QTY | |
|---|---|---|---|---|---|---|---|---|---|---|
| S1 | Smith | 20 | London | S1 | P1 | 200 | ||||
| S2 | Jones | 10 | Paris | S1 | P1 | 700 | ||||
| S3 | Blake | 30 | Paris | S2 | P3 | J1 | 400 | |||
| S4 | Clark | 20 | London | S2 | P3 | J2 | 200 | |||
| S5 | Adams | 30 | Athens | S2 | P3 | J3 | 200 | |||
| S2 | P3 | J4 | 500 | |||||||
| (P) | P# | PNAME | COLOR | WEIGHT | CITY | S2 | P3 | J5 | 600 | |