이제 상황에 따라 JOIN을 적용해보자.
사용될 테이블은 총 4가지로, employees, departments, locations, jobs이다.
1. employees
2. departments
3. locations
4. jobs
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
|
use mydb;
select * from employees;
select * from departments;
select * from locations;
-- 문제1) 사원들의 이름, 부서번호, 부서명을 출력하라
-- employees 테이블의 department_id는 Null값도 있고, 10부터 110까지만 존재
-- but, departments 테이블의 department_id는 10부터 270까지 존재.
-- inner join을 하게되면 department_id는 10부터 110까지 출력.
select first_name, e.department_id, department_name
from employees e inner join departments d on e.department_id = d.department_id;
-- left join을 하게되면 employees의 테이블의 department_id NULL 행 하나와 department_id는 10부터 110까지 출력.
select first_name, e.department_id, department_name
from employees e left join departments d on e.department_id = d.department_id;
-- right join을 하게되면 departments의 테이블의 department_name 행들과 department_id는 10부터 110까지 출력
select first_name, e.department_id, department_name
from employees e right join departments d on e.department_id = d.department_id;
-- left join, right join 모두 department_id 컬럼이 두 테이블이 공통으로 가지고 있는 값에 해당하므로 공통 부분만큼만 출력(10 ~ 110)
select e.first_name, e.department_id, d.department_id, d.department_name
from employees e, departments d
where e.department_id = d.department_id;
-- 문제2) 30번 부서의 사원들의 이름,직업(id),부서명을 출력하라
select first_name, job_id, department_name, e.department_id
from employees e inner join departments d on e.department_id = d.department_id
where e.department_id = 30;
-- 문제2-1) 30번 부서의 사원들의 이름,직업명,부서명을 출력하라
select first_name, job_title, department_name
from employees e inner join departments d on e.department_id = d.department_id
inner join jobs j on e.job_id = j.job_id
where e.department_id = 30;
select e.department_id, first_name, e.job_id, j.job_title, department_name
from employees e, departments d, jobs j
where e.department_id = d.department_id
and e.department_id = 30
and e.job_id = j.job_id;
-- 문제3) 커미션을 받는 사원의 이름, 직업, 부서번호, 부서명을 출력하라
select first_name, job_id, e.department_id, department_name, commission_pct
from employees e inner join departments d on e.department_id = d.department_id
where commission_pct is not null;
select first_name, job_id, e.department_id, department_name, commission_pct
from employees e, departments d
where e.department_id = d.department_id
and commission_pct is not null;
-- 문제4) 지역번호 2500 에서 근무하는 사원의 이름, 직업, 부서번호, 부서명을 출력하라
select first_name, job_id, d.department_id, department_name, d.location_id
from employees e join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id
where d.location_id = 2500;
select first_name, job_id, e.department_id, department_name, d.location_id, l.city
from employees e, departments d, locations l
where e.department_id = d.department_id
and l.location_id = 2500
and d.location_id = l.location_id;
-- 문제5) 이름에 A가 들어가는 사원들의 이름과 부서이름을 출력하라
select first_name, department_name
from employees e join departments d on e.department_id = d.department_id
where first_name like "%A%";
select first_name, department_name
from employees e, departments d
where e.department_id = d.department_id
and first_name like '%A%';
-- 문제6) 사원이름과 그 사원의 관리자 이름을 출력하라
select emp.first_name as 사원, mgr.first_name as 상사
from employees emp, employees mgr
where emp.manager_id = mgr.employee_id;
-- 문제7) 사원이름과 부서명과 월급을 출력하는데 월급이 6000 이상인 사원을 출력하라
select first_name, department_name, salary
from employees e join departments d on e.department_id = d.department_id
where salary >= 6000;
select first_name, department_name, salary
from employees e, departments d
where e.department_id = d.department_id
and salary >= 6000;
-- 문제8) first_name 이 TJ 이란 사원보다 늦게 입사한 사원의 이름과 입사일을 출력하라
select tj.first_name, tj.hire_date, e.first_name, e.hire_date
from employees e, employees tj
where tj.first_name = "TJ" and tj.hire_date < e.hire_date;
-- 문제9) 급여가 3000에서 5000사이인 사원의 이름과 소속부서명 출력하라
select first_name, department_name, salary
from employees e join departments d on e.department_id = d.department_id
where salary between 3000 and 5000;
select e.first_name, e.salary, d.department_name
from employees e, departments d
where e.department_id = d.department_id
-- and e.salary >= 3000 and e.salary <= 5000
and e.salary between 3000 and 5000;
-- 문제10) ACCOUNTING 부서 소속 사원의 이름과 입사일 출력하라
select first_name, hire_date, department_name
from employees e join departments d on e.department_id = d.department_id
where department_name = "ACCOUNTING";
select first_name, department_name, hire_date
from employees e, departments d
where e.department_id = d.department_id
and d.department_name = 'Accounting';
-- 문제11) 급여가 3000이하인 사원의 이름과 급여, 근무지를 출력하라
select first_name, salary, city
from employees e join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id
where salary <= 3000;
select first_name, salary, department_name, city
from employees e, departments d, locations l
where e.department_id = d.department_id
and d.location_id = l.location_id
and salary <= 3000;
-- 문제12) 지역별로 근무하는 사원의 수가 5명 이하인 경우, 사원이 적은 도시순으로 정렬하고
-- 사원수와 city를 출력하시오.
select d.location_id, count(employee_id), city
from employees e join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id
group by l.location_id
having count(employee_id) <= 5
order by count(employee_id) asc;
select d.location_id, count(e.employee_id) as emp_count, l.city
from employees e left outer join departments d
on e.department_id = d.department_id
inner join locations l
on d.location_id = l.location_id
group by d.location_id
having count(e.employee_id) <= 5
order by emp_count asc;
-- 문제13) 지정한 부서번호, 사원이름 및 지정한 사원과 동일한 부서에서 근무하는 모든 사원을 표시하도록 하고
-- 부서번호는 deptno, 사원이름은 사원, 동일한 부서에서 근무하는 사원은 동료로 표시하시오.
-- (부서번호, 사원이름, 동료 순으로 오름차순 정렬)
select e.department_id as deptno, e.first_name as 사원, c.first_name as 동료
from employees e, employees c
where e.department_id = c.department_id and e.first_name != c.first_name
order by deptno asc, 사원 asc, 동료 asc;
select e.first_name as "사원",
e.department_id as deptno, c.department_id as deptno,
c.first_name as "동료"
from employees e join employees c
on e.department_id = c.department_id
where e.employee_id != c.employee_id
-- and e.first_name = 'TJ'
order by e.department_id, e.first_name, c.first_name;
|
cs |
'DB > MySQL' 카테고리의 다른 글
MySQL - JOIN(1) (0) | 2023.01.05 |
---|---|
MySQL - 문법정리(2) - where, order, group, having... (0) | 2023.01.03 |
MySQL 기초 정리 (0) | 2023.01.03 |
MySQL 문법(1) - select * from, insert into values 등등 (0) | 2023.01.02 |