이번 레포트는 책의 Exercise를 푸는 문제였습니다. 그런데 손으로 푸는 문제를 스캔하지 않고 바로 제출해버렸습니다.OTL.. 그래서 SQL을 만드는 문제 풀이만 올립니다.

  문제는 교재인 Database Management Systems입니다. 구글링을 하니 구글에서 해당 책의 스캔본을 제공하는군요.

c1

  그래서 거기에서 문제를 가져왔습니다.

  테스트 환경은 Oracle 9i입니다.

exercise_5_4.sql

  1: -- DDL
  2: create table E54Emp (eid number, ename varchar(20), age number, salary number, PRIMARY KEY(eid));
  3: create table E54Works (eid number, did number, pct_time number, PRIMARY KEY(eid, did));
  4: create table E54Dept (did number, dname varchar(20), budget number, managerid number, PRIMARY KEY(did), FOREIGN KEY(managerid) REFERENCES E54Emp);
  5: 
  6: -- Sample Data Creation
  7: insert into E54Emp (eid, ename, age, salary) values	(108,'NoSyu',25,1650000);
  8: insert into E54Emp (eid, ename, age, salary) values	(122,'DUSTIN',45,630000);
  9: insert into E54Emp (eid, ename, age, salary) values	(191,'CHRISTOPER',24,240000);
 10: insert into E54Emp (eid, ename, age, salary) values	(195,'BOB',63,1680000);
 11: insert into E54Emp (eid, ename, age, salary) values	(229,'BRUTUS',33,240000);
 12: insert into E54Emp (eid, ename, age, salary) values	(331,'LUBBER',55,960000);
 13: insert into E54Emp (eid, ename, age, salary) values	(369,'SMITH'  ,23,240000);
 14: insert into E54Emp (eid, ename, age, salary) values	(432,'ANDY',25,600000);
 15: insert into E54Emp (eid, ename, age, salary) values	(499,'ALLEN'  ,24,480000);
 16: insert into E54Emp (eid, ename, age, salary) values	(500,'DAVE',29,540000);
 17: insert into E54Emp (eid, ename, age, salary) values	(521,'WARD'   ,23,375000);
 18: insert into E54Emp (eid, ename, age, salary) values	(558,'RUSTY',35,750000);
 19: insert into E54Emp (eid, ename, age, salary) values	(566,'JONES'  ,32,892500);
 20: insert into E54Emp (eid, ename, age, salary) values	(650,'SMITH'  ,29,570000);
 21: insert into E54Emp (eid, ename, age, salary) values	(654,'MARTIN' ,24,375000);
 22: insert into E54Emp (eid, ename, age, salary) values	(664,'HORATIO',35,630000);
 23: insert into E54Emp (eid, ename, age, salary) values	(666,'JONES'  ,28,510000);
 24: insert into E54Emp (eid, ename, age, salary) values	(688,'SMITH'  ,28,540000);
 25: insert into E54Emp (eid, ename, age, salary) values	(698,'BLAKE'  ,31,855000);
 26: insert into E54Emp (eid, ename, age, salary) values	(771,'ZORBA',26,750000);
 27: insert into E54Emp (eid, ename, age, salary) values	(782,'CLARK'  ,27,735000);
 28: insert into E54Emp (eid, ename, age, salary) values	(788,'SCOTT'  ,45,900000);
 29: insert into E54Emp (eid, ename, age, salary) values	(831,'MADAYAN',21,240000);
 30: insert into E54Emp (eid, ename, age, salary) values	(832,'GULDU',22,270000);
 31: insert into E54Emp (eid, ename, age, salary) values	(839,'KING'   ,50,2100000);
 32: insert into E54Emp (eid, ename, age, salary) values	(844,'TURNER' ,26,450000);
 33: insert into E54Emp (eid, ename, age, salary) values	(874,'HORATIO',35,690000);
 34: insert into E54Emp (eid, ename, age, salary) values	(876,'ADAMS'  ,25,330000);
 35: insert into E54Emp (eid, ename, age, salary) values	(900,'JAMES'  ,22,285000);
 36: insert into E54Emp (eid, ename, age, salary) values	(902,'FORD'   ,43,900000);
 37: insert into E54Emp (eid, ename, age, salary) values	(911,'JACOB',23,240000);
 38: insert into E54Emp (eid, ename, age, salary) values	(912,'MICHAEL',24,270000);
 39: insert into E54Emp (eid, ename, age, salary) values	(913,'ETHAN',23,270000);
 40: insert into E54Emp (eid, ename, age, salary) values	(914,'JOSHUA',22,240000);
 41: insert into E54Emp (eid, ename, age, salary) values	(915,'DANIEL',20,270000);
 42: insert into E54Emp (eid, ename, age, salary) values	(916,'ALEXANDER',24,240000);
 43: insert into E54Emp (eid, ename, age, salary) values	(917,'ANTHONY',23,270000);
 44: insert into E54Emp (eid, ename, age, salary) values	(918,'WILLIAM',22,240000);
 45: insert into E54Emp (eid, ename, age, salary) values	(919,'MATTHEW',23,270000);
 46: insert into E54Emp (eid, ename, age, salary) values	(934,'MILLER' ,30,390000);
 47: insert into E54Emp (eid, ename, age, salary) values	(985,'ART',25,450000);
 48: insert into E54Emp (eid, ename, age, salary) values	(990,'ELI',26,480000);
 49: insert into E54Emp (eid, ename, age, salary) values	(992,'KYLE',27,480000);
 50: insert into E54Emp (eid, ename, age, salary) values	(994,'DOMINIC',30,480000);
 51: insert into E54Emp (eid, ename, age, salary) values	(996,'ALEX',32,495000);
 52: 
 53: insert into E54Works (eid, did, pct_time) values (839,40,100);
 54: insert into E54Works (eid, did, pct_time) values (108,20,100);
 55: insert into E54Works (eid, did, pct_time) values (369,10,100);
 56: insert into E54Works (eid, did, pct_time) values (900,20,100);
 57: insert into E54Works (eid, did, pct_time) values (499,30,50);
 58: insert into E54Works (eid, did, pct_time) values (499,40,50);
 59: insert into E54Works (eid, did, pct_time) values (521,10,30);
 60: insert into E54Works (eid, did, pct_time) values (521,30,70);
 61: insert into E54Works (eid, did, pct_time) values (566,10,70);
 62: insert into E54Works (eid, did, pct_time) values (566,20,30);
 63: insert into E54Works (eid, did, pct_time) values (654,40,100);
 64: insert into E54Works (eid, did, pct_time) values (698,10,70);
 65: insert into E54Works (eid, did, pct_time) values (698,30,30);
 66: insert into E54Works (eid, did, pct_time) values (782,10,30);
 67: insert into E54Works (eid, did, pct_time) values (782,20,30);
 68: insert into E54Works (eid, did, pct_time) values (788,10,80);
 69: insert into E54Works (eid, did, pct_time) values (788,40,20);
 70: insert into E54Works (eid, did, pct_time) values (844,30,100);
 71: insert into E54Works (eid, did, pct_time) values (876,30,70);
 72: insert into E54Works (eid, did, pct_time) values (876,40,30);
 73: insert into E54Works (eid, did, pct_time) values (902,30,80);
 74: insert into E54Works (eid, did, pct_time) values (902,40,20);
 75: insert into E54Works (eid, did, pct_time) values (934,30,50);
 76: insert into E54Works (eid, did, pct_time) values (934,20,50);
 77: insert into E54Works (eid, did, pct_time) values (122,20,100);
 78: insert into E54Works (eid, did, pct_time) values (229,10,100);
 79: insert into E54Works (eid, did, pct_time) values (331,10,50);
 80: insert into E54Works (eid, did, pct_time) values (331,20,50);
 81: insert into E54Works (eid, did, pct_time) values (432,30,100);
 82: insert into E54Works (eid, did, pct_time) values (558,40,100);
 83: insert into E54Works (eid, did, pct_time) values (664,20,100);
 84: insert into E54Works (eid, did, pct_time) values (771,20,50);
 85: insert into E54Works (eid, did, pct_time) values (771,10,50);
 86: insert into E54Works (eid, did, pct_time) values (874,20,70);
 87: insert into E54Works (eid, did, pct_time) values (874,30,30);
 88: insert into E54Works (eid, did, pct_time) values (985,10,70);
 89: insert into E54Works (eid, did, pct_time) values (985,30,30);
 90: insert into E54Works (eid, did, pct_time) values (195,50,100);
 91: insert into E54Works (eid, did, pct_time) values (500,20,100);
 92: insert into E54Works (eid, did, pct_time) values (666,20,100);
 93: insert into E54Works (eid, did, pct_time) values (688,20,100);
 94: insert into E54Works (eid, did, pct_time) values (650,20,100);
 95: insert into E54Works (eid, did, pct_time) values (831,20,100);
 96: insert into E54Works (eid, did, pct_time) values (832,20,100);
 97: insert into E54Works (eid, did, pct_time) values (911,20,100);
 98: insert into E54Works (eid, did, pct_time) values (912,20,100);
 99: insert into E54Works (eid, did, pct_time) values (913,20,100);
100: insert into E54Works (eid, did, pct_time) values (914,20,100);
101: insert into E54Works (eid, did, pct_time) values (915,20,100);
102: insert into E54Works (eid, did, pct_time) values (916,20,100);
103: insert into E54Works (eid, did, pct_time) values (917,20,100);
104: insert into E54Works (eid, did, pct_time) values (918,20,100);
105: insert into E54Works (eid, did, pct_time) values (191,20,100);
106: insert into E54Works (eid, did, pct_time) values (919,20,100);
107: insert into E54Works (eid, did, pct_time) values (990,50,100);
108: insert into E54Works (eid, did, pct_time) values (992,50,100);
109: insert into E54Works (eid, did, pct_time) values (994,50,100);
110: insert into E54Works (eid, did, pct_time) values (996,50,100);
111: 
112: insert into E54Dept (did, dname, budget, managerid) values (10,'Hardware',800000 ,788);
113: insert into E54Dept (did, dname, budget, managerid) values (20,'Software',1500000 ,108);
114: insert into E54Dept (did, dname, budget, managerid) values (30,'business',3000000 ,902);
115: insert into E54Dept (did, dname, budget, managerid) values (40,'management',7000000 ,839);
116: insert into E54Dept (did, dname, budget, managerid) values (50,'test',5500000 ,108);
117: 
118: 	
119: -- SQL
120: -- 1. Print the names and ages of each employee who works in both the Hardware department and the Software department.
121: select e.ename, e.age
122: from E54Emp e, E54Works w1, E54Works w2, E54Dept d1, E54Dept d2
123: where e.eid = w1.eid and w1.did = d1.did and d1.dname = 'Hardware' and e.eid = w2.eid and w2.did = d2.did and d2.dname = 'Software';
124: 
125: -- 2. For each department with more than 20 full-time-equivalent employees (i.e., where the part-time and full-time employees add up to at least that many full-time employees), print the did together with the number of employees that work in that department.
126: 
127: select w.did, COUNT(w.eid)
128: from E54Works w
129: group by w.did
130: having 2000 < (select sum(w1.pct_time) from E54Works w1 where w.did = w1.did);
131: 
132: -- 3. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in.
133: select e.ename
134: from E54Emp e
135: where e.salary > ALL (select d.budget from E54Works w, E54Dept d where w.did = d.did and e.eid = w.eid);
136: 
137: -- 4. Find the managerids of managers who manage only departments with budgets greater than $1 million.
138: select distinct d.managerid
139: from E54Dept d
140: where 1000000 < d.budget;
141: 
142: -- 5. Find the enames of managers who manage the departments with the largest budgets.
143: select e.ename
144: from E54Emp e, E54Dept d
145: where d.managerid = e.eid and d.budget = (select max(d1.budget) from E54Dept d1);
146: 
147: -- 6. If a manager manages more than one department, he or she controls the sum of all the budgets for those departments. Find the managerids of managers who control more than $5 million.
148: select d.managerid
149: from E54Dept d
150: where 5000000 < (select sum(d1.budget) from E54Dept d1 where d1.managerid = d.managerid);
151: 
152: -- 7. Find the managerids of managers who control the largest amounts.
153: select dTemp.managerid
154: from ( select distinct d1.managerid, sum(d1.budget) as d1_budget
155: 		from E54Dept d1
156: 		group by d1.managerid ) dTemp
157: where dTemp.d1_budget = (select max(d2_budget)
158: 						from ( select distinct d2.managerid, sum(d2.budget) as d2_budget
159: 								from E54Dept d2
160: 								group by d2.managerid ) dTemp);
161: 
162: -- 8. Find the enames of managers who manage only departments with budgets larger than $1 million, but at least one department with budget less than $5 million.
163: select e.ename
164: from E54Emp e, E54Dept d
165: where e.eid = d.managerid and d.budget > 1000000 and 5000000 > any (select d1.budget from E54Dept d1 where d1.did = d.did);
166: 
167: -- C. Find the name of employee whose salary is larger than all his managers.
168: select e.ename
169: from E54Emp e
170: where e.salary > all (select e1.salary from E54Emp e1 where e1.eid in (select d2.managerid from E54Dept d2, E54works w2 where d2.did = w2.did and w2.eid = e.eid));
171: 
172: 
173: -- Table 제거
174: drop table E54Works;
175: drop table E54Dept;
176: drop table E54Emp;

result.sql

  1: SQL> select * from e54emp;
  2: 
  3:        EID ENAME                       AGE     SALARY
  4: ---------- -------------------- ---------- ----------
  5:        108 NoSyu                        25    1650000
  6:        122 DUSTIN                       45     630000
  7:        191 CHRISTOPER                   24     240000
  8:        195 BOB                          63    1680000
  9:        229 BRUTUS                       33     240000
 10:        331 LUBBER                       55     960000
 11:        369 SMITH                        23     240000
 12:        432 ANDY                         25     600000
 13:        499 ALLEN                        24     480000
 14:        500 DAVE                         29     540000
 15:        521 WARD                         23     375000
 16: 
 17:        EID ENAME                       AGE     SALARY
 18: ---------- -------------------- ---------- ----------
 19:        558 RUSTY                        35     750000
 20:        566 JONES                        32     892500
 21:        650 SMITH                        29     570000
 22:        654 MARTIN                       24     375000
 23:        664 HORATIO                      35     630000
 24:        666 JONES                        28     510000
 25:        688 SMITH                        28     540000
 26:        698 BLAKE                        31     855000
 27:        771 ZORBA                        26     750000
 28:        782 CLARK                        27     735000
 29:        788 SCOTT                        45     900000
 30: 
 31:        EID ENAME                       AGE     SALARY
 32: ---------- -------------------- ---------- ----------
 33:        831 MADAYAN                      21     240000
 34:        832 GULDU                        22     270000
 35:        839 KING                         50    2100000
 36:        844 TURNER                       26     450000
 37:        874 HORATIO                      35     690000
 38:        876 ADAMS                        25     330000
 39:        900 JAMES                        22     285000
 40:        902 FORD                         43     900000
 41:        911 JACOB                        23     240000
 42:        912 MICHAEL                      24     270000
 43:        913 ETHAN                        23     270000
 44: 
 45:        EID ENAME                       AGE     SALARY
 46: ---------- -------------------- ---------- ----------
 47:        914 JOSHUA                       22     240000
 48:        915 DANIEL                       20     270000
 49:        916 ALEXANDER                    24     240000
 50:        917 ANTHONY                      23     270000
 51:        918 WILLIAM                      22     240000
 52:        919 MATTHEW                      23     270000
 53:        934 MILLER                       30     390000
 54:        985 ART                          25     450000
 55:        990 ELI                          26     480000
 56:        992 KYLE                         27     480000
 57:        994 DOMINIC                      30     480000
 58: 
 59:        EID ENAME                       AGE     SALARY
 60: ---------- -------------------- ---------- ----------
 61:        996 ALEX                         32     495000
 62: 
 63: 45 개의 행이 선택되었습니다.
 64: 
 65: SQL> select * from e54works;
 66: 
 67:        EID        DID   PCT_TIME
 68: ---------- ---------- ----------
 69:        839         40        100
 70:        108         20        100
 71:        369         10        100
 72:        900         20        100
 73:        499         30         50
 74:        499         40         50
 75:        521         10         30
 76:        521         30         70
 77:        566         10         70
 78:        566         20         30
 79:        654         40        100
 80: 
 81:        EID        DID   PCT_TIME
 82: ---------- ---------- ----------
 83:        698         10         70
 84:        698         30         30
 85:        782         10         30
 86:        782         20         30
 87:        788         10         80
 88:        788         40         20
 89:        844         30        100
 90:        876         30         70
 91:        876         40         30
 92:        902         30         80
 93:        902         40         20
 94: 
 95:        EID        DID   PCT_TIME
 96: ---------- ---------- ----------
 97:        934         30         50
 98:        934         20         50
 99:        122         20        100
100:        229         10        100
101:        331         10         50
102:        331         20         50
103:        432         30        100
104:        558         40        100
105:        664         20        100
106:        771         20         50
107:        771         10         50
108: 
109:        EID        DID   PCT_TIME
110: ---------- ---------- ----------
111:        874         20         70
112:        874         30         30
113:        985         10         70
114:        985         30         30
115:        195         50        100
116:        500         20        100
117:        666         20        100
118:        688         20        100
119:        650         20        100
120:        831         20        100
121:        832         20        100
122: 
123:        EID        DID   PCT_TIME
124: ---------- ---------- ----------
125:        911         20        100
126:        912         20        100
127:        913         20        100
128:        914         20        100
129:        915         20        100
130:        916         20        100
131:        917         20        100
132:        918         20        100
133:        191         20        100
134:        919         20        100
135:        990         50        100
136: 
137:        EID        DID   PCT_TIME
138: ---------- ---------- ----------
139:        992         50        100
140:        994         50        100
141:        996         50        100
142: 
143: 58 개의 행이 선택되었습니다.
144: 
145: SQL> select * from e54dept;
146: 
147:        DID DNAME                    BUDGET  MANAGERID
148: ---------- -------------------- ---------- ----------
149:         10 Hardware                 800000        788
150:         20 Software                1500000        108
151:         30 business                3000000        902
152:         40 management              7000000        839
153:         50 test                    5500000        108
154: 
155: 		
156: -- 1. Print the names and ages of each employee who works in both the Hardware department and the Software department.
157: SQL> select e.ename, e.age
158:   2  from E54Emp e, E54Works w1, E54Works w2, E54Dept d1, E54Dept d2
159:   3  where e.eid = w1.eid and w1.did = d1.did and d1.dname = 'Hardware' and e.eid = w2.eid and w2.did = d2.did and d2.dname = 'Software';
160: 
161: ENAME                       AGE
162: -------------------- ----------
163: JONES                        32
164: CLARK                        27
165: LUBBER                       55
166: ZORBA                        26
167: 
168: -- 2. For each department with more than 20 full-time-equivalent employees (i.e., where the part-time and full-time employees add up to at least that many full-time employees), print the did together with the number of employees that work in that department.
169: SQL> select w.did, COUNT(w.eid)
170:   2  from E54Works w
171:   3  group by w.did
172:   4  having 2000 < (select sum(w1.pct_time) from E54Works w1 where w.did = w1.did);
173: 
174:        DID COUNT(W.EID)
175: ---------- ------------
176:         20           26
177: 
178: -- 3. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in.
179: SQL> select e.ename
180:   2  from E54Emp e
181:   3  where e.salary > ALL (select d.budget from E54Works w, E54Dept d where w.did = d.did and e.eid = w.eid);
182: 
183: ENAME
184: --------------------
185: NoSyu
186: 
187: 
188: -- 4. Find the managerids of managers who manage only departments with budgets greater than $1 million.
189: SQL> select distinct d.managerid
190:   2  from E54Dept d
191:   3  where 1000000 < d.budget;
192: 
193:  MANAGERID
194: ----------
195:        108
196:        839
197:        902
198: 
199: 
200: -- 5. Find the enames of managers who manage the departments with the largest budgets.
201: SQL> select e.ename
202:   2  from E54Emp e, E54Dept d
203:   3  where d.managerid = e.eid and d.budget = (select max(d1.budget) from E54Dept d1);
204: 
205: ENAME
206: --------------------
207: KING
208: 
209: -- 6. If a manager manages more than one department, he or she controls the sum of all the budgets for those departments. Find the managerids of managers who control more than $5 million.
210: SQL> select d.managerid
211:   2  from E54Dept d
212:   3  where 5000000 < (select sum(d1.budget) from E54Dept d1 where d1.managerid = d.managerid);
213: 
214:  MANAGERID
215: ----------
216:        108
217:        839
218:        108
219: 
220: -- 7. Find the managerids of managers who control the largest amounts.
221: SQL> select dTemp.managerid
222:   2  from ( select distinct d1.managerid, sum(d1.budget) as d1_budget
223:   3    from E54Dept d1
224:   4    group by d1.managerid ) dTemp
225:   5  where dTemp.d1_budget = (select max(d2_budget)
226:   6        from ( select distinct d2.managerid, sum(d2.budget) as d2_budget
227:   7          from E54Dept d2
228:   8          group by d2.managerid ) dTemp);
229: 
230:  MANAGERID
231: ----------
232:        108
233:        839
234: 
235: -- 8. Find the enames of managers who manage only departments with budgets larger than $1 million, but at least one department with budget less than $5 million.
236: SQL> select e.ename
237:   2  from E54Emp e, E54Dept d
238:   3  where e.eid = d.managerid and d.budget > 1000000 and 5000000 > any (select d1.budget from E54Dept d1 where d1.did = d.did);
239: 
240: ENAME
241: --------------------
242: NoSyu
243: FORD
244: 
245: -- C. Find the name of employee whose salary is larger than all his managers.
246: SQL> select e.ename
247:   2  from E54Emp e
248:   3  where e.salary > all (select e1.salary from E54Emp e1 where e1.eid in (select d2.managerid from E54Dept d2, E54works w2 where d2.did = w2.did and w2.eid = e.eid));
249: 
250: ENAME
251: --------------------
252: BOB
253: 
254: 
크리에이티브 커먼즈 라이선스
Creative Commons License

글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.

트랙백 주소 :: http://nosyu.pe.kr/trackback/2159

댓글을 달아 주세요

[로그인][오픈아이디란?]