-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.sql
More file actions
188 lines (161 loc) · 5.32 KB
/
07.sql
File metadata and controls
188 lines (161 loc) · 5.32 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
-- 7장 subquery
-- query 안에 subquery를 사용한다.
select last_name, salary
from employees
where salary > (select salary
from employees
where last_name = 'Abel');
select last_name, job_id, salary
from employees
where job_id = (select job_id
from employees
where last_name = 'Ernst')
and salary > (select salary
from employees
where last_name = 'Ernst');
-- 과제] Kochhar 에게 보고하는 사원들의 이름, 직업을 조회하라.
select last_name, job_id
from employees
where manager_id = (select employee_id
from employees
where last_name = 'Kochhar');
-- 과제] IT 부서에서 일하는 사원들의 부서번호, 이름, 직업을 조회하라.
select department_id, last_name, job_id
from employees
where department_id = (select department_id
from departments
where department_name = 'IT');
-- 과제] Abel과 같은 부서에서 일하는 동료들의 이름, 입사일을 조회하라.
-- 이름 순으로 정렬한다.
select last_name, hire_date
from employees
where department_id = (select department_id
from employees
where last_name = 'Abel')
and last_name <> 'Abel'
order by 1;
select last_name, salary
from employees
where salary > (select salary
from employees
where last_name = 'King'); -- error: record가 2개가 나온다.
select last_name, job_id, salary
from employees
where salary = (select min(salary)
from employees);
select department_id, min(salary)
from employees
group by department_id
having min(salary) > (select min(salary)
from employees
where department_id = 50);
-- 과제] 회사 평균 월급 이상 버는 사원들의 사번, 이름, 월급을 조회하라.
-- 월급 내림차순 정렬한다.
select employee_id, last_name, salary
from employees
where salary >= (select avg(salary)
from employees)
order by salary desc;
---
select employee_id, last_name
from employees
where salary = (select min(salary)
from employees
group by department_id); -- error
select employee_id, last_name
from employees
where salary in (select min(salary)
from employees
group by department_id);
-- 과제] 이름에 u가 포함된 사원이 있는 부서에서 일하는 사원들의 사번, 이름을 조회하라.
select employee_id, last_name
from employees
where department_id in (select department_id
from employees
where last_name like '%u%');
-- 과제] 1700번 지역에 위치한 부서에서 일하는 사원들의 이름, 직업, 부서번호를 조회하라.
select last_name, job_id, department_id
from employees
where department_id in (select department_id
from departments
where location_id = 1700);
-- any : 하나라도 true일 경우 true이다.
select employee_id, last_name
from employees
where salary =any (select min(salary)
from employees
group by department_id);
select employee_id, last_name, job_id, salary
from employees
where salary <any (select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
-- all : 모든 값이 true일 경우 true이다.
select employee_id, last_name, job_id, salary
from employees
where salary <all (select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
-- 과제] 60번 부서의 일부 사원보다 급여가 많은 사원들의 이름을 조회하라.
select last_name
from employees
where salary >any (select salary
from employees
where department_id = 60);
-- 과제] 회사 평균 월급보다, 그리고 모든 프로그래머보다 월급을 더 받는
-- 사원들의 이름, 직업, 월급을 조회하라.
select last_name, job_id, salary
from employees
where salary > (select avg(salary)
from employees)
and salary >all (select salary
from employees
where job_id = 'IT_PROG');
---
-- no row
select last_name
from employees
where salary = (select salary
from employees
where employee_id = 1);
select last_name
from employees
where salary in (select salary
from employees
where job_id = 'IT');
-- subquery 일부가 null 일 경우 일치하는 값만 처리한다.
select last_name
from employees
where employee_id in (select manager_id
from employees);
select last_name
from employees
where employee_id not in (select manager_id
from employees);
-- 과제] 위 문장을 all 연산자로 refactoring 하라.
select last_name
from employees
where employee_id !=all (select manager_id
from employees);
---
-- exist
select count(*)
from departments d;
select count(*)
from departments d
where exists (select *
from employees e
where e.department_id = d.department_id);
select count(*)
from departments d
where not exists (select *
from employees e
where e.department_id = d.department_id);
-- 과제] 직업을 바꾼 적이 있는 사원들의 이름, 직업을 조회하라.
select employee_id, last_name, job_id
from employees e
where exists (select *
from job_history j
where e.employee_id = j.employee_id);