You are on page 1of 4

1 /*

2 * Answer to question 1.
3 *
4 * Errata:
5 *
6 * SALE: the final column, 'SDATE' --> 'DATES';
7 */
8
9
10 /*
11 * 1.(a) What has Cilla bought? Assume we only want the type of the item.
12 */
13
14 select distinct item
15 from sale
16 where client='Cilla';
17
18 /*
19 * 1.(b) which staff made sales on March 11 1998?
20 * --- we are simplifying here by using numbers as dates.
21 * -- the column name should be 'sdate' rather than 'date';
22 * 'date' is a reserved word in most DBMS's.
23 */
24
25 select distinct staff
26 from sale
27 where sdate=19980311;
28
29 /*
30 * 1.(c) Wha items are out of stock?
31 * -- we only want the type of items
32 * -- 'out of stock' means 'numstock=0' in the stock table or no info at all
33 */
34
35 select type
36 from stock
37 where numstock=0
38 union
39 select type
40 from item
41 where type not in
42 (select type from stock);
43
44 /*
45 * 1.(d) What items havenot been sold?
46 * -- item types
47 */
48
49 select type
50 from item
51 where type not in (select item from sale);
52
53
54 /*
55 * 1.(e) Which clerks don't have any sales?
56 */
57
58 select name
59 from staff
60 where position='Clerk'
61 and
62 name not in (select staff from sale);
63
64 /*
65 * 1.(f) What categories of staff have made sales?
66 */
67
68 select distinct position
69 from staff, sale
70 where staff.name=sale.staff;
71
72 /*
73 * or
74 */
75
76 select distinct position
77 from staff
78 where name in (select staff from sale);
79
80
81 /*
82 * 1.(g) Have any sales of any item exceeded the current stock of that item?
83 */
84
85 select item
86 from sale
87 group by item
88 having sum(numsold) >
89 (select numstock
90 from stock
91 where stock.type=item);
92
93
94 /*
95 * answers to question 2.
96 */
97
98 /*
99 * 2.(a) what is the potential value of all current stock?
100 */
101
102 select stock.type, numstock*item.price
103 from stock, item
104 where stock.type=item.type;
105
106 /*
107 * 2.(b) Which staff have ONLY sold items worth at least $10?
108 */
109
110 /*
111 * Incorrect answer: Explain why.
112 */
113 select distinct staff
114 from sale, item
115 where sale.item=item.type
116 and
117 item.price>10;
118
119
120 /*
121 * Correct answer (1).
122 * --- 'minus' is the set difference operator.
123 */
124 select distinct staff
125 from sale
126 minus
127 select distinct staff
128 from sale, item
129 where sale.item=item.type
130 and
131 item.price<10;
132
133 /*
134 * correct answer (2)
135 */
136 select staff
137 from sale, item
138 where sale.item=item.type
139 group by staff
140 having min(item.price)>=10;
141
142 /*
143 * Question 2(c)
144 * How much money has each client spent with each staff member?
145 */
146
147 select client, staff, sum ( price * numsold) as Total
148 from item, sale
149 where item.type=sale.item
150 group by client, staff;
151
152
153 /*
154 * Question 2(d)
155 * Which customer has spent the most money?
156 */
157
158 create view spending (customer, spent)
159 as
160 select client,sum( price * numsold)
161 from sale, item
162 where item.type = sale.item
163 group by client;
164
165 select *
166 from spending
167 where spent =
168 (select max(spent)
169 from spending);
170
171
172 /*
173 * Question 2(e).
174 * For each item where sales so far exceed current stock,
175 * how much money will be lost if no further stock is acquired
176 * and there are no further sales?
177 * --- assumption: due to lacking communication between the sales people
178 * people in charge of stock, we may have more sales than those in stock. *
179 */
180
181
182 create view totalsale(type, soldtotal)
183 as
184 select item, sum(numsold)
185 from sale
186 group by item;
187
188
189 select stock.type,(soldtotal- numstock) * price as lost
190 from totalsale, item, stock
191 where totalsale.type = stock.type and
192 stock.type = item.type and
193 soldtotal > numstock;
194
195
196 /*
197 * Question 3.
198 * Define a view to show,
199 * for each staff member, their total volume of sales
200 * (in dollars, in number of items sold, in number of clients)
201 */
202
203 create view staffsaletotal (staff,totaldollar, totalitem, totalclient)
204 as
205 select staff, sum(numsold*price), sum(numsold), count(distinct client)
206 from item, sale
207 where item.type = sale.item
208 group by staff;
209
210 /*
211 * Question 4 (a)
212 */
213 --- list the distinct positions of the staff who have sales record
214
215 /*
216 * Question 4 (b)
217 */
218 -- list the client names and the number of staff with whom they have sales
219
220 /*
221 * Question 4 (c).
222 */
223
224 -- Give the name of those staff members who
225 -- have serviced all the clients.
226
227
228 /*
229 * Questions 4(d).
230 */
231
232 -- Which staff has the most sales in dollars?
233
234
235 /*
236 * Question 4(d) without using view.
237 */
238
239 select staff, sum(item.price*sale.numsold) as totalsale
240 from sale, item
241 where item.type=sale.item
242 group by staff
243 having sum(item.price*sale.numsold) >= all
244 (select sum(item.price*sale.numsold)
245 from sale, item
246 where item.type=sale.item
247 group by staff);
248
249
250 /*
251 * Question 5.
252 */
253
254 -- (a), (b) and (c) are equivalent:
255 -- Select the clients who have bought items of price less than $100.
256
257
258

You might also like