You are on page 1of 6

A Guide to SQL, Eighth Edition

Solutions 5-1

Chapter 5: Multiple-Table Queries


Solutions
Answers to Premiere Products Exercises
1.
SELECT ORDER_NUM, ORDER_DATE, ORDERS.CUSTOMER_NUM, CUSTOMER_NAME
FROM ORDERS, CUSTOMER
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM;

ORDER_NUM
21608
21610
21613
21614
21617
21619
21623

ORDER_DATE
20-Oct-10
20-Oct-10
21-Oct-10
21-Oct-10
23-Oct-10
23-Oct-10
23-Oct-10

CUSTOMER_NUM
148
356
408
282
608
148
608

CUSTOMER_NAME
Al's Appliance and Sport
Ferguson's
The Everything Shop
Brookings Direct
Johnson's Department Store
Al's Appliance and Sport
Johnson's Department Store

2.
SELECT ORDER_NUM, ORDERS.CUSTOMER_NUM, CUSTOMER_NAME
FROM ORDERS, CUSTOMER
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = '23-Oct-2010';

ORDER_NUM
21617
21619
21623

CUSTOMER_NUM
608
148
608

CUSTOMER_NAME
Johnson's Department Store
Al's Appliance and Sport
Johnson's Department Store

In Access, the command is:


SELECT ORDER_NUM, ORDERS.CUSTOMER_NUM, CUSTOMER_NAME
FROM ORDERS, CUSTOMER
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = #10/23/2010#;
In SQL Server, the command is:
SELECT ORDER_NUM, ORDERS.CUSTOMER_NUM, CUSTOMER_NAME
FROM ORDERS, CUSTOMER
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = '2010-10-23';
3.
SELECT ORDERS.ORDER_NUM, ORDER_DATE, PART_NUM, NUM_ORDERED, QUOTED_PRICE
FROM ORDERS, ORDER_LINE
WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM;

ORDER_NUM
21608
21610
21610
21613
21614
21617
21617

ORDER_DATE
20-Oct-10
20-Oct-10
20-Oct-10
21-Oct-10
21-Oct-10
23-Oct-10
23-Oct-10

PART_NUM
AT94
DR93
DW11
KL62
KT03
BV06
CD52

NUM_ORDERED
11
1
1
4
2
2
4

QUOTED_PRICE
21.95
495
399.99
329.95
595
794.95
150

A Guide to SQL, Eighth Edition

21619
21623

23-Oct-10
23-Oct-10

Solutions 5-2

DR93
KV29

1
2

4.
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = '23-Oct-2010');

CUSTOMER_NUM CUSTOMER_NAME
148
Al's Appliance and Sport
608
Johnson's Department Store
In Access, the command is:
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = #10/23/2010#);

In SQL Server, the command is:


SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = '2010-10-23');
5.
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE EXISTS
(SELECT *
FROM ORDERS
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = '23-Oct-2010');

CUSTOMER_NUM CUSTOMER_NAME
148
Al's Appliance and Sport
608
Johnson's Department Store
In Access, the command is:
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE EXISTS
(SELECT *
FROM ORDERS
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = #10/23/2010#);
In SQL Server, the command is:
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE EXISTS
(SELECT *
FROM ORDERS
WHERE ORDERS.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM
AND ORDER_DATE = '2010-10-23');
6.
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM NOT IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = '23-Oct-2010');

495
1290

A Guide to SQL, Eighth Edition

CUSTOMER_NUM
282
356
408
462
524
687
725
842

Solutions 5-3

CUSTOMER_NAME
Brookings Direct
Ferguson's
The Everything Shop
Bargains Galore
Kline's
Lee's Sport and Appliance
Deerfield's Four Seasons
All Season Shop

In Access, the command is:


SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM NOT IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = #10/23/2010#);
In SQL Server, the command is:
SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTOMER_NUM NOT IN
(SELECT CUSTOMER_NUM
FROM ORDERS
WHERE ORDER_DATE = '2010-10-23');
7.
SELECT ORDERS.ORDER_NUM, ORDER_DATE, PART.PART_NUM, DESCRIPTION, CLASS
FROM ORDERS, ORDER_LINE, PART
WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM
AND ORDER_LINE.PART_NUM = PART.PART_NUM;

ORDER_NUM
21608
21610
21610
21613
21614
21617
21617
21619
21623

ORDER_DATE
20-Oct-10
20-Oct-10
20-Oct-10
21-Oct-10
21-Oct-10
23-Oct-10
23-Oct-10
23-Oct-10
23-Oct-10

PART_NUM
AT94
DR93
DW11
KL62
KT03
BV06
CD52
DR93
KV29

DESCRIPTION
Iron
Gas Range
Washer
Dryer
Dishwasher
Home Gym
Microwave Oven
Gas Range
Treadmill

CLASS
HW
AP
AP
AP
AP
SG
AP
AP
SG

8.
SELECT ORDERS.ORDER_NUM, ORDER_DATE, PART.PART_NUM, DESCRIPTION, CLASS
FROM ORDERS, ORDER_LINE, PART
WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM
AND ORDER_LINE.PART_NUM = PART.PART_NUM
ORDER BY CLASS, ORDERS.ORDER_NUM;

ORDER_NUM
21610
21610
21613
21614
21617
21619
21608
21617
21623

ORDER_DATE
20-Oct-10
20-Oct-10
21-Oct-10
21-Oct-10
23-Oct-10
23-Oct-10
20-Oct-10
23-Oct-10
23-Oct-10

PART_NUM
DR93
DW11
KL62
KT03
CD52
DR93
AT94
BV06
KV29

DESCRIPTION
Gas Range
Washer
Dryer
Dishwasher
Microwave Oven
Gas Range
Iron
Home Gym
Treadmill

CLASS
AP
AP
AP
AP
AP
AP
HW
SG
SG

A Guide to SQL, Eighth Edition

Solutions 5-4

9.
SELECT REP_NUM, LAST_NAME, FIRST_NAME
FROM REP
WHERE REP_NUM IN
(SELECT REP_NUM
FROM CUSTOMER
WHERE CREDIT_LIMIT = 10000);

REP_NUM LAST_NAME
35
Hull
65
Perez

FIRST_NAME
Richard
Juan

10.
SELECT DISTINCT REP.REP_NUM, LAST_NAME, FIRST_NAME
FROM REP, CUSTOMER
WHERE REP.REP_NUM = CUSTOMER.REP_NUM
AND CREDIT_LIMIT = 10000;

REP_NUM LAST_NAME
35
Hull
65
Perez

FIRST_NAME
Richard
Juan

11.
SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER, ORDERS, ORDER_LINE, PART
WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
AND ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM
AND ORDER_LINE.PART_NUM = PART.PART_NUM
AND DESCRIPTION = 'Gas Range';

CUSTOMER_NUM CUSTOMER_NAME
356
Ferguson's
148
Al's Appliance and Sport
12.
SELECT F.PART_NUM, F.DESCRIPTION, S.PART_NUM, S.DESCRIPTION, F.CLASS
FROM PART F, PART S
WHERE F.CLASS = S.CLASS
AND F.PART_NUM < S.PART_NUM
ORDER BY CLASS, F.PART_NUM, S.PART_NUM;

PART_NUM
CD52
CD52
CD52
CD52
DR93
DR93
DR93
DW11
DW11
KL62
AT94
AT94
DL71
BV06

DESCRIPTION
Microwave Oven
Microwave Oven
Microwave Oven
Microwave Oven
Gas Range
Gas Range
Gas Range
Washer
Washer
Dryer
Iron
Iron
Cordless Drill
Home Gym

PART_NUM
DR93
DW11
KL62
KT03
DW11
KL62
KT03
KL62
KT03
KT03
DL71
FD21
FD21
KV29

DESCRIPTION
Gas Range
Washer
Dryer
Dishwasher
Washer
Dryer
Dishwasher
Dryer
Dishwasher
Dishwasher
Cordless Drill
Stand Mixer
Stand Mixer
Treadmill

13.
SELECT ORDERS.ORDER_NUM, ORDER_DATE
FROM CUSTOMER, ORDERS
WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
AND CUSTOMER_NAME = 'Johnson''s Department Store';

ORDER_NUM
21617

ORDER_DATE
23-Oct-10

CLASS
AP
AP
AP
AP
AP
AP
AP
AP
AP
AP
HW
HW
HW
SG

A Guide to SQL, Eighth Edition

21623

Solutions 5-5

23-Oct-10

14.
SELECT ORDERS.ORDER_NUM, ORDER_DATE
FROM ORDERS, ORDER_LINE, PART
WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM
AND ORDER_LINE.PART_NUM = PART.PART_NUM
AND DESCRIPTION = 'Iron';

ORDER_NUM
21608

ORDER_DATE
20-Oct-10

15.
SELECT ORDER_NUM, ORDER_DATE
FROM CUSTOMER, ORDERS
WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
AND CUSTOMER_NAME = 'Johnson''s Department Store'
UNION
SELECT ORDERS.ORDER_NUM, ORDER_DATE
FROM ORDERS, ORDER_LINE, PART
WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM
AND ORDER_LINE.PART_NUM = PART.PART_NUM
AND DESCRIPTION = 'Gas Range';

ORDER_NUM
21610
21617
21619
21623

ORDER_DATE
20-Oct-10
23-Oct-10
23-Oct-10
23-Oct-10

16.
SELECT ORDER_NUM, ORDER_DATE
FROM CUSTOMER, ORDERS
WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
AND CUSTOMER_NAME = 'Johnson''s Department Store'
AND ORDER_NUM IN
(SELECT ORDER_NUM
FROM ORDER_LINE, PART
WHERE ORDER_LINE.PART_NUM = PART.PART_NUM
AND DESCRIPTION = 'Gas Range');
no data found
17.
SELECT ORDER_NUM, ORDER_DATE
FROM CUSTOMER, ORDERS
WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
AND CUSTOMER_NAME = 'Johnson''s Department Store'
AND ORDER_NUM NOT IN
(SELECT ORDER_NUM
FROM ORDER_LINE, PART
WHERE ORDER_LINE.PART_NUM = PART.PART_NUM
AND DESCRIPTION = 'Gas Range');

ORDER_NUM ORDER_DATE
21617
23-Oct-10
21623
23-Oct-10
18.
SELECT PART_NUM, DESCRIPTION, PRICE, CLASS
FROM PART
WHERE PRICE > ALL
(SELECT PRICE
FROM PART
WHERE CLASS = 'AP');

PART_NUM
BV06
KV29

DESCRIPTION PRICE
Home Gym
794.95
Treadmill
1390

CLASS
SG
SG

A Guide to SQL, Eighth Edition

Solutions 5-6

19.
SELECT PART_NUM, DESCRIPTION, PRICE, CLASS
FROM PART
WHERE PRICE > ANY
(SELECT PRICE
FROM PART
WHERE CLASS = 'AP');

PART_NUM
DESCRIPTION PRICE
KV29
Treadmill
1390
BV06
Home Gym
794.95
KT03
Dishwasher
595
DR93
Gas Range
495
DW11
Washer
399.99
KL62
Dryer
349.95
This query answers the question Which parts

CLASS
SG
SG
AP
AP
AP
AP
have a price greater than any price in the class

AP?
20.
SELECT PART.PART_NUM, DESCRIPTION, ON_HAND, NUM_ORDERED
FROM PART
LEFT JOIN ORDER_LINE
ON PART.PART_NUM = ORDER_LINE.PART_NUM
ORDER BY PART.PART_NUM;

PART_NUM
AT94
BV06
CD52
DL71
DR93
DR93
DW11
FD21
KL62
KT03
KV29

DESCRIPTION
Iron
Home Gym
Microwave Oven
Cordless Drill
Gas Range
Gas Range
Washer
Stand Mixer
Dryer
Dishwasher
Treadmill

ON_HAND
50
45
32
21
8
8
12
22
12
8
9

NUM_ORDERED
11
2
4
1
1
1
4
2
2

You might also like