You are on page 1of 8

1 -- John Mar Chang, Juruel Keanu Lorenzo, Cleo Jon Supapo

2
3 1. For each order, list the order number and order date along with the number and
name of the customer that placed the order.
4
5 SELECT
6 o.order_num,
7 o.order_date,
8 o.customer_num,
9 c.customer_name
10 FROM
11 orders o,
12 customer c
13 WHERE
14 o.customer_num = c.customer_num;
15
16 +-----------+------------+--------------+----------------------------+
17 | order_num | order_date | customer_num | customer_name              |
18 +-----------+------------+--------------+----------------------------+
19 | 21608     | 2013-10-20 | 148          | Al's Appliance and Sport   |
20 | 21610     | 2013-10-20 | 356         | Ferguson's                 |
21 | 21613     | 2013-10-21 | 408          | The Everything Shop        |
22 | 21614     | 2013-10-21 | 282          | Brookings Direct           |
23 | 21617     | 2013-10-23 | 608          | Johnson's Department Store |
24 | 21619     | 2013-10-23 | 148         | Al's Appliance and Sport   |
25 | 21623     | 2013-10-23 | 608          | Johnson's Department Store |
26 +-----------+------------+--------------+----------------------------+
27 7 rows in set (0.009 sec)
28
29
30 2. For each order placed on October 21, 2013, list the order number along with the
number and name of the customer that placed the order.
31
32 SELECT
33 o.order_num,
34 o.customer_num,
35 c.customer_name
36 FROM
37 orders o,
38 customer c
39 WHERE
40 c.customer_num = o.customer_num
41 AND o.order_date = '2013-10-21';
42
43 +-----------+--------------+---------------------+
44 | order_num | customer_num | customer_name       |
45 +-----------+--------------+---------------------+
46 | 21613     | 408         | The Everything Shop |
47 | 21614     | 282         | Brookings Direct   |
48 +-----------+--------------+---------------------+
49 2 rows in set (0.001 sec)
50
51
52 3. For each order, list the order number, order date, part number, number of units
ordered, and quoted price for each order line that makes up the order.
53
54 SELECT
55 o.order_num,
56 o.order_date,
57 ol.part_num,
58 ol.num_ordered,
59 ol.quoted_price
60 FROM
61 orders o,
62 order_line ol
63 WHERE
64 o.order_num = ol.order_num;
65
66 +-----------+------------+----------+-------------+--------------+
67 | order_num | order_date | part_num | num_ordered | quoted_price |
68 +-----------+------------+----------+-------------+--------------+
69 | 21608     | 2013-10-20 | AT94     |         11 |       21.95 |
70 | 21610     | 2013-10-20 | DR93     |           1 |       495.00 |
71 | 21610     | 2013-10-20 | DW11     |           1 |       399.99 |
72 | 21613     | 2013-10-21 | KL62     |           4 |       329.95 |
73 | 21614     | 2013-10-21 | KT03     |           2 |       595.00 |
74 | 21617     | 2013-10-23 | BV06     |           2 |       794.95 |
75 | 21617     | 2013-10-23 | CD52     |           4 |       150.00 |
76 | 21619     | 2013-10-23 | DR93     |           1 |       495.00 |
77 | 21623     | 2013-10-23 | KV29     |           2 |     1290.00 |
78 +-----------+------------+----------+-------------+--------------+
79 9 rows in set (0.001 sec)
80
81
82 4. Use the IN operator to find the number and name of each customer that placed an
order on October 21, 2013.
83
84 SELECT
85 customer_num,
86 customer_name
87 FROM
88 customer
89 WHERE
90 customer_num IN (
91   SELECT
92     customer_num
93   FROM
94     orders
95   WHERE
96     order_date = '2013-10-21'
97 );
98
99 +--------------+---------------------+
100 | customer_num | customer_name       |
101 +--------------+---------------------+
102 | 282         | Brookings Direct   |
103 | 408         | The Everything Shop |
104 +--------------+---------------------+
105 2 rows in set (0.002 sec)
106
107
108 5. Repeat Exercise 4, but this time use the EXISTS operator in your answer.
109
110 SELECT
111 c.customer_num,
112 c.customer_name
113 FROM
114 customer c
115 WHERE
116 EXISTS (
117   SELECT
118     *
119   FROM
120     orders o
121   WHERE
122     o.customer_num = c.customer_num
123     AND o.order_date = '2013-10-21'
124 );
125
126 +--------------+---------------------+
127 | customer_num | customer_name       |
128 +--------------+---------------------+
129 | 282         | Brookings Direct   |
130 | 408         | The Everything Shop |
131 +--------------+---------------------+
132 2 rows in set (0.002 sec)
133
134
135 6. Find the number and name of each customer that did not place an order on October
21, 2013.
136
137 SELECT
138 customer_num,
139 customer_name
140 FROM
141 customer
142 WHERE
143 customer_num IN (
144   SELECT
145     customer_num
146   FROM
147     orders
148   WHERE
149     order_date != '2013-10-21'
150 );
151
152 +--------------+----------------------------+
153 | customer_num | customer_name             |
154 +--------------+----------------------------+
155 | 148         | Al's Appliance and Sport   |
156 | 356          | Ferguson's                 |
157 | 608         | Johnson's Department Store |
158 +--------------+----------------------------+
159 3 rows in set (0.003 sec)
160
161
162 7. For each order, list the order number, order date, part number, part description,
and item class for, each part that makes up the order.
163
164 SELECT
165 o.order_num,
166 o.order_date,
167 p.part_num,
168 p.description,
169 p.class
170 FROM
171 orders o,
172 order_line ol,
173 part p
174 WHERE
175 o.order_num = ol.order_num
176 AND ol.part_num = p.part_num;
177
178 +-----------+------------+----------+----------------+-------+
179 | order_num | order_date | part_num | description    | class |
180 +-----------+------------+----------+----------------+-------+
181 | 21608     | 2013-10-20 | AT94     | Iron           | HW    |
182 | 21610     | 2013-10-20 | DR93     | Gas Range      | AP    |
183 | 21610     | 2013-10-20 | DW11     | Washer         | AP    |
184 | 21613     | 2013-10-21 | KL62     | Dryer          | AP    |
185 | 21614     | 2013-10-21 | KT03     | Dishwasher     | AP    |
186 | 21617     | 2013-10-23 | BV06     | Home Gym       | SG    |
187 | 21617     | 2013-10-23 | CD52     | Microwave Oven | AP    |
188 | 21619     | 2013-10-23 | DR93     | Gas Range      | AP    |
189 | 21623     | 2013-10-23 | KV29     | Treadmill      | SG    |
190 +-----------+------------+----------+----------------+-------+
191 9 rows in set (0.001 sec)
192
193
194 8. Repeat Exercise 7, but this time order the rows by item class and then by order
number.
195
196 SELECT
197 o.order_num,
198 o.order_date,
199 p.part_num,
200 p.description,
201 p.class
202 FROM
203 orders o,
204 order_line ol,
205 part p
206 WHERE
207 o.order_num = ol.order_num
208 AND ol.part_num = p.part_num
209 ORDER BY
210 p.class,
211 o.order_num;
212
213 +-----------+------------+----------+----------------+-------+
214 | order_num | order_date | part_num | description    | class |
215 +-----------+------------+----------+----------------+-------+
216 | 21610     | 2013-10-20 | DW11     | Washer         | AP    |
217 | 21610     | 2013-10-20 | DR93     | Gas Range      | AP    |
218 | 21613     | 2013-10-21 | KL62     | Dryer          | AP    |
219 | 21614     | 2013-10-21 | KT03     | Dishwasher     | AP    |
220 | 21617     | 2013-10-23 | CD52     | Microwave Oven | AP    |
221 | 21619     | 2013-10-23 | DR93     | Gas Range      | AP    |
222 | 21608     | 2013-10-20 | AT94     | Iron           | HW    |
223 | 21617     | 2013-10-23 | BV06     | Home Gym       | SG    |
224 | 21623     | 2013-10-23 | KV29     | Treadmill      | SG    |
225 +-----------+------------+----------+----------------+-------+
226 9 rows in set (0.001 sec)
227
228
229 9. Use a subquery to find the rep number, last name, and first name of each sales rep
who represents at least one customer with a credit limit of $5,000. List each sales
rep only once in the
230 results.
231
232 SELECT
233 DISTINCT rep_num,
234 last_name,
235 first_name
236 FROM
237 rep
238 WHERE
239 rep_num IN (
240   SELECT
241     rep_num
242   FROM
243     customer
244   WHERE
245     credit_limit = 5000
246  );
247
248 +---------+-----------+------------+
249 | rep_num | last_name | first_name |
250 +---------+-----------+------------+
251 | 35      | Hull      | Richard    |
252 +---------+-----------+------------+
253 1 row in set (0.001 sec)
254
255
256 10. Repeat Exercise 9, but this time do not use a subquery.
257
258 SELECT
259 DISTINCT r.rep_num,
260 last_name,
261 first_name
262 FROM
263 rep r,
264 customer c
265 WHERE
266 r.rep_num = c.rep_num
267 AND c.credit_limit = 5000;
268
269 +---------+-----------+------------+
270 | rep_num | last_name | first_name |
271 +---------+-----------+------------+
272 | 35      | Hull      | Richard    |
273 +---------+-----------+------------+
274 1 row in set (0.002 sec)
275
276
277 11. Find the number and name of each customer that currently has an order on file for
a Gas Range.
278
279 SELECT
280 customer_num,
281 customer_name
282 FROM
283 customer
284 WHERE
285 customer_num IN (
286   SELECT
287     o.customer_num
288   FROM
289     orders o,
290     order_line ol,
291     part p
292   WHERE
293     o.order_num = ol.order_num
294     AND ol.part_num = p.part_num
295     AND p.description = 'Gas Range'
296  );
297
298 +--------------+--------------------------+
299 | customer_num | customer_name            |
300 +--------------+--------------------------+
301 | 148          | Al's Appliance and Sport |
302 | 356         | Ferguson's               |
303 +--------------+--------------------------+
304 2 rows in set (0.001 sec)
305
306
307 12. List the part number, part description, and item class for each pair of parts
that are in the same item class. (For example, one such pair would be part AT94 and
part FD21, because the item class
308 for both parts is HW.)
309
310 SELECT
311 a.part_num as 'A part_num',
312 a.description as 'A description',
313 b.part_num as 'B part_num',
314 b.description as 'B description',
315 a.class
316 FROM
317 part a,
318 part b
319 WHERE
320 a.part_num < b.part_num
321 AND a.class = b.class
322 ORDER BY
323 a.class;
324
325 +----------+----------------+---------+-------+-----------+---------+
326 | part_num | description    | on_hand | class | warehouse | price   |
327 +----------+----------------+---------+-------+-----------+---------+
328 | AT94     | Iron           |      50 | HW    | 3         |   24.95 |
329 | BV06     | Home Gym       |      45 | SG    | 2         |  794.95 |
330 | CD52     | Microwave Oven |      32 | AP    | 1         |  165.00 |
331 | DL71     | Cordless Drill |      21 | HW    | 3         |  129.95 |
332 | DR93     | Gas Range      |       8 | AP    | 2         |  495.00 |
333 | DW11     | Washer         |      12 | AP    | 3         |  399.99 |
334 | FD21     | Stand Mixer    |      22 | HW    | 3         |  159.95 |
335 | KL62     | Dryer          |      12 | AP    | 1         |  349.95 |
336 | KT03     | Dishwasher     |       8 | AP    | 3         |  595.00 |
337 | KV29     | Treadmill      |       9 | SG    | 2         | 1390.00 |
338 +----------+----------------+---------+-------+-----------+---------+
339 10 rows in set (0.001 sec)
340
341
342 13. List the order number and order date for each order placed by the customer named
Johnson's Department Store. (Hint: To enter an apostrophe (single quotation mark)
within a string of characters, type two single quotation marks.)
343
344 SELECT
345 order_num,
346 order_date
347 FROM
348 orders
349 WHERE
350 customer_num IN (
351   SELECT
352     customer_num
353   FROM
354     customer
355   WHERE
356     customer_name = "Johnson's Department Store"
357  );
358
359 +-----------+------------+
360 | order_num | order_date |
361 +-----------+------------+
362 | 21617     | 2013-10-23 |
363 | 21623     | 2013-10-23 |
364 +-----------+------------+
365 2 rows in set (0.003 sec)
366
367
368 14. List the order number and order date for each order that contains an order line
for an Iron.
369
370 SELECT
371 order_num,
372 order_date
373 FROM
374 orders
375 WHERE
376 order_num IN (
377   SELECT
378     order_num
379   FROM
380     order_line ol,
381     part p
382   WHERE
383     ol.part_num = p.part_num
384     AND p.description = 'Iron'
385  );
386
387 +-----------+------------+
388 | order_num | order_date |
389 +-----------+------------+
390 | 21608     | 2013-10-20 |
391 +-----------+------------+
392 1 row in set (0.001 sec)
393
394
395 15. List the order number and order date for each order that either was placed by
Johnson's Department Store or that contains an order line for a Gas Range.
396
397 SELECT DISTINCT
398 o.order_num,
399 o.order_date
400 FROM
401 orders o,
402 order_line ol,
403 part p,
404 customer c
405 WHERE
406 c.customer_num = o.customer_num
407 AND o.order_num = ol.order_num
408 AND ol.part_num = p.part_num
409 AND (
410   c.customer_name = "Johnson's Department Store"
411   OR p.description = "Gas Range"
412  );
413
414 +-----------+------------+
415 | order_num | order_date |
416 +-----------+------------+
417 | 21610     | 2013-10-20 |
418 | 21617     | 2013-10-23 |
419 | 21619     | 2013-10-23 |
420 | 21623     | 2013-10-23 |
421 +-----------+------------+
422 4 rows in set (0.002 sec)

You might also like