-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp242.sql
More file actions
269 lines (224 loc) · 5.15 KB
/
Copy pathp242.sql
File metadata and controls
269 lines (224 loc) · 5.15 KB
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
--서브쿼리
--SCOTT사원이 소속된 부서이름을 출력
select deptno
from emp
where ename = 'SCOTT';
select dname
from dept
where deptno = 20;
select dname
from dept
where deptno = (
select deptno
from emp
where ename = 'SCOTT'
);
--단일행 서브쿼리 사용 1
select *
from emp
where ename = 'ALLEN';
select comm
from emp
where comm > 300;
select *
from emp;
--SCOTT과 같은 부서에서 근무하는 사원의 이름과 부서의 번호를 출력
select ename, deptno
from emp
where deptno = (
select deptno
from emp
where ename = 'SCOTT') and ename <> 'SCOTT';
--SCOTT과 동일한 직급을 가진 사원의 정보를 출력
select *
from emp
where job = (
select job
from emp
where ename = 'SCOTT') and ename <> 'SCOTT';
--SCOTT의 급여와 동일하거나 많이 받는 사원의 이름과 급여를 출력
select ename, sal
from emp
where sal >= (
select sal
from emp
where ename = 'SCOTT') and ename <> 'SCOTT';
--DALLAS에서 근무하는 사원의 이름, 부서번호를 출력
select ename, deptno
from emp
where deptno = (
select deptno
from dept
where loc = 'DALLAS'
);
--SALES 부서에서 근무하는 모든 사원의 이름과 급여를 출력
select ename, sal
from emp
where deptno = (
select deptno
from dept
where dname = 'SALES'
);
select *
from emp;
select *
from dept;
select *
from salgrade;
--자신의 매니저가 KING인 사원의 이름과 급여를 출력
select ename, sal
from emp
where mgr = (
select empno
from emp
where ename = 'KING'
);
--다중행 서브쿼리
select *
from emp
where deptno = 20; --단일행 결과 출력
select *
from emp
where deptno in(10, 20); --다중행 결과 출력(in 연산자)
select *
from emp
where sal = any( --다중행 결과 출력(any 연산자)
select max(sal)
from emp
group by deptno
);
select *
from emp
where sal = some( --다중행 결과 출력(some 연산자)
select max(sal)
from emp
group by deptno
);
select *
from emp
where sal = all( --다중행 결과 출력(all 연산자)
select max(sal)
from emp
group by deptno
);
select *
from emp
where sal < 2850;
select *
from emp
where deptno = 30;
select *
from emp
where sal < (
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where sal < any(
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where sal > any(
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where sal < all(
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where sal > all(
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where sal < 2850;
select min(sal)
from emp
where sal < 2850
order by sal;
select max(sal)
from emp
where sal < 2850
order by sal;
select max(sal)
from emp
group by deptno;
--exists 연산자
select *
from emp
where exists( --다중행 결과 출력(exists연산자)
select sal
from emp
where deptno = 30) order by sal, empno;
select *
from emp
where deptno = 30;
select *
from emp
where exists( --다중행 결과 출력(exists연산자)
select sal
from emp
where deptno = 40) order by sal, empno;
--in 연산자 사용
--1. 부서별로 가장 급여가 많은 사원의 사원번호, 사원명, 급여, 부서번호를 출력
select empno, ename, sal, deptno
from emp
where sal in(
select max(sal)
from emp
group by deptno
);
--2. 직급이 MANAGER인 사람이 속한 부서의 부서번호, 부서명, 지역을 출력
select deptno, dname, loc
from dept
where deptno in (
select deptno
from emp
where job = 'MANAGER'
);
--3. 30번 부서의 사원들 중에서 급여를 가장 많이 받는 사원보다 더 많은 급여를 받는 사원의 이름과 급여를 출력
select ename, sal
from emp
where sal > (
select max(sal)
from emp
where deptno in(30)
);
--any 연산자 사용
--1. 영업사원(SALESMAN)들의 최소 급여보다 많이 받는 사원들의 이름과 급여, 직급을 출력하되 영업사원은 제외
select ename, sal, job
from emp
where sal > any(
select min(sal)
from emp
where job = 'SALESMAN') and job <> 'SALESMAN';
--2. 직급이 SALESMAN인 사원이 받는 급여들의 최대급여 보다 많이 받는 사원들의 이름과 급여를 출력하되, 부서번호가 20번인 사원은 제외
select ename, sal, deptno
from emp
where sal > any(
select max(sal)
from emp
where job = 'SALESMAN') and not deptno = 20;
--all 연산자 사용
--1. 영업사원(SALESMAN)들의 최소 급여보다 많이 받는 사원들의 이름과 급여, 직급을 출력하되 영업사원은 제외
select ename, sal, job
from emp
where sal > all(
select min(sal)
from emp
where job = 'SALESMAN') and job <> 'SALESMAN';
--2. 직급이 SALESMAN인 사원이 받는 급여들의 최대급여 보다 많이 받는 사원들의 이름과 급여를 출력하되, 부서번호가 20번인 사원은 제외
select ename, sal, deptno
from emp
where sal > all(
select max(sal)
from emp
where job = 'SALESMAN') and not deptno = 20;