-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2020.12.03
329 lines (272 loc) · 8.52 KB
/
2020.12.03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
--6.서브쿼리,세미조인,안티조인
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.department_name
from departments b
where a.department_id=b.department_id) dept_name
from employees a
order by 1;
--스칼라서브쿼리에서 본체와 조인을 안해주면 부서명전체를 가져오므로 오류
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.department_name
from departments b
) dept_name
from employees a
order by 1;
--스칼라서브쿼리에서 select 다음 값에 2개이상시 두개의 칼럼을
--가져오므로 오류
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.department_name,b.location_id
from departments b
where a.department_id=b.department_id) dept_name
from employees a
order by 1;
--job_title,job_id 두칼럼을 사용하지만, 문자열 연결연산자로
--결합되어 최종반환값은 1개
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.job_title||'('||b.job_id||')'
from jobs b
where a.job_id=b.job_id) job_names
from employees a
order by 1;
--조인과 스칼라서브쿼리 비교
--조인함수 사용시 178번사원 kimberly누락 but
--서브쿼리에서는 조회됨
--서브쿼리사용시a테이블 온전히 나오고 서브쿼리부분만 조인한 데이터가 나옴
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
b.department_name
from employees a,
departments b
where a.department_id = b.department_id
order by 1;
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.department_name
from departments b
where a.department_id=b.department_id) dept_name
from employees a
order by 1;
--left조인
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
b.department_name
from employees a
left join departments b
on a.department_id = b.department_id
order by 1;
--스칼라서브쿼리2
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
(select b.department_name
from departments b
where a.department_id=b.department_id
) dept_name,
(select d.country_name
from departments b,
locations c,
countries d
where a.department_id=b.department_id
and b.location_id = c.location_id
and c.country_id = d.country_id
) country_name
from employees a
order by 1;
--인라인뷰
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
c.dept_name
from employees a,
(select b.department_id,
b.department_name dept_name
from departments b)c
where a.department_id = c.department_id
order by 1;
--인라인뷰는 서브쿼리내에서 조인 불가능
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.department_id,
c.dept_name
from employees a,
(select b.department_id,
b.department_name dept_name
from departments b
where a.department_id = b.department_id)c
order by 1;
--인라인뷰2
select a.employee_id,
a.first_name||''||a.last_name emp_name,
dept.department_name,
loc.street_address,
loc.city,
loc.country_name
from employees a,
(select * from departments b) dept,
(select l.location_id,
l.street_address,
l.city,
c.country_name
from locations l,
countries c
where l.country_id = c.country_id) loc
where a.department_id = dept.department_id
and dept.location_id = loc.location_id
order by 1;
--인라인뷰3
select a.department_id,
a.employee_id,
a.last_name,a.salary,
k.department_id second_dept_id,
k.avg_salary
from employees a,
(select b.department_id,round(AVG(b.salary),0) avg_salary
from employees b
group by b.department_id) k
where a.department_id = k.department_id
order by a.department_id;
--중첩서브쿼리
select * from departments where department_id
in (select department_id from employees);
select * from departments a where exists
(select 1 from employees b where a.department_id = b.department_id);
----------
select * from departments a
where exists(select 'A' from employees b
where a.department_id = b.department_id
and b.salary > 10000);
--중첩서브쿼리. 구문 이해가 안감(??????????????????????)
select employee_id,
first_name||''||last_name emp_name,
job_id,
salary
from employees
where (job_id,salary) in (select job_id,min_salary from jobs)
order by 1;
--중첩서브쿼리
select last_name,
employee_id,
salary+NVL(commission_pct,0),
job_id,
e.department_id
from employees e,
departments d
where e.department_id = d.department_id
and salary + NVL(commission_pct,0)
> (select salary+NVL(commission_pct,0)
from employees
where last_name = 'Pataballa')
order by last_name,employee_id;
--중첩 서브쿼리
select department_id,employee_id,last_name,salary
from employees a
where salary > (select AVG(salary)
from employees b
where a.department_id = b.department_id)--이부분이 어떻게 group by역할이되는건지..
order by 1;
--세미조인
select * from departments
where department_id IN(select department_id
from employees);
select * from departments a
where exists (select 1 from employees b
where a.department_id=b.department_id);
--안티조인
--사원이속하지않은 부서만 추출
select * from departments a
where not exists (select 1 from employees b
where a.department_id=b.department_id);
--job_history에없는 사원을추출
select a.employee_id,
a.first_name||''||a.last_name
from employees a
where a.employee_id
NOT IN (select employee_id from job_history)
order by 1;
------------------------
--------------------------------------------------
--Q.1
--AVG_salary는job_id 별 사원의 평균급여
--서브쿼리에서 굳이 셀프조인하는이유가??
select a.employee_id,
a.first_name||''||a.last_name emp_name,
a.job_id,
a.salary,
(select AVG(b.salary) from employees b where a.job_id = b.job_id
group by b.job_id) avg_salary
from employees a;
--Q.2
select b.department_name,loc.street_address,loc.country_name
from departments b,
LATERAL
(select l.location_id,l.street_address,c.country_name
from locations l,
countries c
where 1=1
and l.country_id = c.country_id
and b.location_id = l.location_id) loc
;
--Q.3
select employee_id,
job_id,
salary
from employees a
where exists (select 1 from jobs b
where 1=1
and a.job_id = b.job_id
and a.salary=b.min_salary);
----------------------------------------------
select job_id from jobs
intersect
select job_id from employees;
--교차검색시 더 다양한 직업들이있는데 왜 안나오는지???
--Q.4
select *
from departments a
where not exists
(select 1 from employees b
where a.department_id=b.department_id);
--Q.5
select * from covid19;
--스칼라로 실패
select to_char(a.DATES,'YYYY-MM') MONTHS,
a.continent,
a.country,
sum(a.new_cases) new_cases,
c.continent_cases
round(a.new_cases/c.continent_cases)*100,2) rates
from covid19 a
,(select sum(b.new_cases) continent_cases,
to_char(b.dates,'yyyy-mm') months
from covid19 b
group by to_char(b.dates,'yyyy-mm'),b.continent) c
where a.months = c.months
and a.continent = c.continent
group by to_char(a.dates,'yyyy-mm'),a.continent,a.country
order by 1;
-----------------------------------------------------
----------------------------------------------------
--Q.6
--왜 썸하고 NVL하는지? NVL하고 썸해야되는게 아닌지??
select * from covid19;
select to_char(dates,'mm') months,
sum(nvl(new_tests,0)) 검사수,
sum(new_cases) 확진자수,
(nvl(sum(new_cases),0)/nvl(sum(new_tests),0)100 확진율
from covid19
where 1=1
and iso_code='kor'
and to_char(dates,'yyyy')='2020'
group by to_char(dates,'mm')
order by 1;