[DB] ORACLE - 3์ผ
in DB
ORACLE
์ค๋ผํด 3์ผ
โป select (ํด์์์ 1 โ 2 โ 3 โ 4 โ 5)
select [distinct] [์ปฌ๋ผ1,์ปฌ๋ผ2,.....][as ๋ณ๋ช
][ || ์ฐ์ฐ์][*] --- 6
from ํ
์ด๋ธ๋ช
--- 1
[where ์กฐ๊ฑด์ ] --- 2
[group by์ปฌ๋ผ๋ช
] --- 3
[having ์กฐ๊ฑด์ ] --- 4
[order by ์ปฌ๋ผ๋ช
asc|desc ] --- 5
group by : ๊ทธ๋ฃนํจ์(max,min,sum,avg,count..)์ ๊ฐ์ด ์ฌ์ฉ
having : ๋ฌถ์ด๋์ ๊ทธ๋ฃน์ ์กฐ๊ฑด์
ex1) ์ฌ์ํ ์ด๋ธ์์ ๊ธ์ฌ์ ํ๊ท ์ ๊ตฌํ์์ค
์กฐ๊ฑด)์์์ดํ๋ ์ ์ญ, ์ธ์๋ฆฌ๋ง๋ค ์ฝค๋ง(,)
์ฌ์๊ธ์ฌํ๊ท
------------
6,461
select to_char(trunc(avg(salary),0), '99,999') as ์ฌ์๊ธ์ฌํ๊ท from employees;
ex2)๋ถ์๋ณ ๊ธ์ฌํ๊ท ์ ๊ตฌํ์์ค
์กฐ๊ฑด1) ์์์ดํ๋ ๋ฐ์ฌ๋ฆผ
์กฐ๊ฑด2) ์ธ์๋ฆฌ๋ง๋ค์ฝค๋ง, ํํ๋จ์ \๋ฅผ ํ์
์กฐ๊ฑด3) ๋ถ์์ฝ๋ ํ๊ท ๊ธ์ฌ
---------------------------
10 8,600
์กฐ๊ฑด4) ๋ถ์๋ณ๋ก ์ค๋ฆ์ฐจ์์ ๋ ฌํ์์ค
์กฐ๊ฑด5) ํ๊ท ๊ธ์ฌ๊ฐ 5000์ด์์ธ ๋ถ์๋ง ํ์ํ์์ค
select department_id as ๋ถ์์ฝ๋,
to_char(round(avg(salary),0),'L99,999,999') as ํ๊ท ๊ธ์ฌ
from employees
group by department_id
having avg(salary)>=5000
order by department_id asc;
ex3) ๋ถ์๋ณ ๊ธ์ฌํ๊ท ์ ๊ตฌํด์ ์ฌ์๋ช (last_name),๋ถ์๋ณ ๊ธ์ฌํ๊ท ์ ์ถ๋ ฅํ์์ค - X
select last_name, avg(salary)
from employees
group by department_id;
โ
last_name ๋๋ฌธ์ error
group by์ ์ ์๋๊ฒ์ select์ ์กฐํํ๋ฉด error
[๋ฌธ์ 1] ์ ๋ฌดID๋ณ ๊ธ์ฌ์ ํฉ๊ณ๋ฅผ ๊ตฌํด์ ์ ๋ฌดID, ๊ธ์ฌํฉ๊ณ๋ฅผ ์ถ๋ ฅํ์์ค
ex4) ๋นํจ์จ์ ์ธ having์
10๊ณผ 20 ๋ถ์์์ ์ต๋๊ธ์ฌ๋ฅผ ๋ฐ๋์ฌ๋์ ์ต๋๊ธ์ฌ๋ฅผ ๊ตฌํ์ฌ ์ ๋ ฌํ์์ค
department_id max_salary
---------------------------
10 4400
20 13000
select department_id, max(salary) as max_salary
from employees
group by department_id
having department_id in(10,20)
order by department_id;
์ ์ฒด ๋ถ์์ ๋ํด ๊ทธ๋ฃน์ ์ก์์ ์ต๋๊ธ์ฌ๋ฅผ ๊ตฌํํ์ ๋ถ์๊ฐ 10 ๊ณผ 20์ธ๊ฒ์ ์ถ๋ ค๋ด๊ธฐ ๋๋ฌธ์ธ๊ฒ
select department_id, max(salary) as max_salary
from employees
where department_id in(10,20)
group by department_id
order by department_id;
๋ถ์๋ฒํธ๊ฐ 10๊ณผ 20์ธ ๊ฒ๋ง ๊ณจ๋ผ๋ด์ ๊ทธ๋ฃน์ก๊ธฐ ๋๋ฌธ์ ์๋๊ฐ ์ข ๋น ๋ฅด๋ค
[์กฐ์ธ(join)]
(employees) (departments) (join)
์ฌ ๋ถ ๋ถ ๋ถ ์ฌ ๋ถ ๋ถ
์ ์ + ์ ์ = ์ ์ ์
์ด ๋ฒ ๋ฒ ์ด ์ด ๋ฒ ์ด
๋ฆ ํธ ํธ ๋ฆ ๋ฆ ํธ ๋ฆ
โป ์ข
๋ฅ
1. Inner join : ๊ฐ์๊ฒ ๋ผ๋ฆฌ๋ง ์ฐ๊ฒฐ
2. Outer join : ํ์ชฝ์ ๊ธฐ์ค(๋ชจ๋ํฌํจ)ํด์ ์ฐ๊ฒฐ
left join : ์ผ์ชฝ์ปฌ๋ผ ๋ชจ๋ํฌํจ
right join : ์ค๋ฅธ์ชฝ์ปฌ๋ฌ ๋ชจ๋ํฌํจ
3. full join : ์ผ์ชฝ,์ค๋ฅธ์ชฝ ๋ชจ๋ ํฌํจ
4. self join : ์๊ธฐ์์ ํ
์ด๋ธ๊ณผ ์ฐ๊ฒฐ
5. cross join : ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ก ์ฐ๊ฒฐ
6. non equijoin : ๋ฒ์์ ์ํ๋์ง ์ฌ๋ถ๋ฅผ ํ์ธ
7. n๊ฐ ํ
์ด๋ธ ์กฐ์ธ : ์ฌ๋ฌ๊ฐ์ ํ
์ด๋ธ ์กฐ์ธ
โป ๋ฐฉ๋ฒ
1. ์ค๋ผํด ๊ตฌ๋ฌธ์ ์ฉ
2. Ansi ํ์ค๊ตฌ๋ฌธ
ex5) inner join : ๊ฐ์๊ฒ๋ผ๋ฆฌ๋ง ์กฐ์ธ
์ฌ์ํ ์ด๋ธ๊ณผ ๋ถ์ํ ์ด๋ธ์์ ๋ถ์๊ฐ ๊ฐ์๊ฒฝ์ฐ ์ฌ์๋ฒํธ,๋ถ์๋ฒํธ,๋ถ์์ด๋ฆ์ ์ถ๋ ฅํ์์ค
๋ฐฉ๋ฒ1(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
select employee_id,
employees.department_id,
department_name
from employees, departments
where employees.department_id = departments.department_id;
๋ฐฉ๋ฒ2(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
select e.employee_id,
e.department_id,
d.department_name
from employees e, departments d
where e.department_id = d.department_id;
๋ฐฉ๋ฒ3(Ansiํ์ค)
select employee_id, department_id, department_name
from employees
join departments using(department_id);
[๋ฌธ์ 2] ๋ถ์ํ ์ด๋ธ(departments d)๊ณผ ์์นํ ์ด๋ธ(Locations l)์ ์ฐ๊ฒฐํ์ฌ ๋ถ์๊ฐ ์์นํ ๋์๋ฅผ ์์๋ด์์ค
department_id city
----------------------
10 Seattle
๋ฐฉ๋ฒ2(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
๋ฐฉ๋ฒ3(Ansiํ์ค)
ex6) outer join(left) : ์ผ์ชฝ ํ ์ด๋ธ์ ๋ชจ๋ํฌํจํ์ฌ ์กฐ์ธ
์ฌ์ํ ์ด๋ธ๊ณผ ๋ถ์ํ ์ด๋ธ์์ ๋ถ์๋ฒํธ๊ฐ ๊ฐ์ ์ฌ์์ ์กฐ์ธํ์์ค ===> 107๋ ์ฝ๋
๋ฐฉ๋ฒ1(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
select e.last_name, d.department_id, d.department_name
from employees e, departments d
where e.department_id=d.department_id(+);
๋ฐฉ๋ฒ2(Ansiํ์ค)
select last_name, department_id, department_name
from employees
left join departments using(department_id);
ex7) outer join(right) : ์ค๋ฅธ์ชฝ ํ ์ด๋ธ์ ๋ชจ๋ํฌํจํ์ฌ ์กฐ์ธ
๋ฐฉ๋ฒ1(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
select e.last_name, d.department_id, d.department_name
from employees e, departments d
where e.department_id(+)=d.department_id;
๋ฐฉ๋ฒ2(Ansiํ์ค)
select last_name, department_id, department_name
from employees
right join departments using(department_id);
๋ฐฉ๋ฒ1(์ค๋ผํด์ ์ฉ๊ตฌ๋ฌธ)
select d.department_id, l.city
from departments d, locations2 l
where d.location_id=l.loc_id;
๋ฐฉ๋ฒ2(Ansiํ์ค)
select department_id, city
from departments
join locations2 on(location_id=loc_id);
๋ฐฉ๋ฒ3(Ansiํ์ค)
select department_id, city
from departments d
join locations2 l on(d.location_id=l.loc_id);
ex11) self ์กฐ์ธ : ์๊ธฐ์์ ์ ํ ์ด๋ธ๊ณผ ์กฐ์ธํ๋๊ฒฝ์ฐ ์ฌ์๊ณผ ๊ด๋ฆฌ์๋ฅผ ์ฐ๊ฒฐํ์์ค
์ฌ์๋ฒํธ ์ฌ์์ด๋ฆ ๊ด๋ฆฌ์
-------------------------
101 Kochhar King
employees employees
employee_id, last_name(์ฌ์์ด๋ฆ) last_name(๊ด๋ฆฌ์)
์กฐ๊ฑด employee_id = manager_id
select employee_id,manager_id, last_name from employees; โ e
select employee_id, last_name from employees; โ m
[๋ฐฉ๋ฒ 1]
select e.employee_id as ์ฌ์๋ฒํธ, e.last_name as ์ฌ์์ด๋ฆ, m.last_name as ๊ด๋ฆฌ์
from employees e, employees m
where m.employee_id=e.manager_id;
[๋ฐฉ๋ฒ 2]
select e.employee_id as ์ฌ์๋ฒํธ, e.last_name as ์ฌ์์ด๋ฆ, m.last_name as ๊ด๋ฆฌ์
from employees e
join employees m on(m.employee_id=e.manager_id);
ex12) cross join : ๋ชจ๋ ํ์ ๋ํด ๊ฐ๋ฅํ ๋ชจ๋ ์กฐํฉ์ ์์ฑํ๋ ์กฐ์ธ
select * from countries, locations;โ 575๋ ์ฝ๋
select * from countries cross join locations;
ex13) Non Equijoin (๋ ์ดํ์กฐ์ธ)
โ : ์ปฌ๋ผ๊ฐ์ด ๊ฐ์๊ฒฝ์ฐ๊ฐ ์๋ ๋ฒ์์ ์ํ๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํ ๋
โ on ( ์ปฌ๋ผ๋ช between ์ปฌ๋ผ๋ช 1 and ์ปฌ๋ผ๋ช 2)
create table salgrade(
salvel varchar2(2),
lowst number,
highst number);
insert into salgrade values('A', 20000, 29999);
insert into salgrade values('B', 10000, 19999);
insert into salgrade values('C', 0, 9999);
commit;
select * from salgrade;
select last_name, salary, salvel
from employees
join salgrade on(salary between lowst and highst)
order by salary desc;
ex14) n(์ฌ๋ฌ)๊ฐ์ ํ ์ด๋ธ์ ์กฐ์ธ
์ ๋ฌดID๊ฐ์ ์ฌ์๋ค์ ์ฌ์์ด๋ฆ, ์ ๋ฌด๋ด์ฉ, ๋ถ์์ด๋ฆ์ ์ถ๋ ฅํ์์ค
(employees, jobs, departmentsํ ์ด๋ธ์ ์กฐ์ธ)
<๋ถ์>
employees jobs departments
--------------------------------
department_id job_id department_id
job_id
<์ถ๋ ฅ>
last_name job_title department_name
-----------------------------------------
select last_name, job_title, department_name
from employees
join departments using(department_id)
join jobs using(job_id);
[๋ฌธ์ 3] ์์นID, ๋ถ์ID๋ฅผ ์ฐ๊ฒฐํด์ ์ฌ์์ด๋ฆ,๋์,๋ถ์์ด๋ฆ์ ์ถ๋ ฅํ์์ค
โ (๊ด๋ จํ ์ด๋ธ : employees, locations2, departments)
์กฐ๊ฑด1 : ์ฌ์์ด๋ฆ ,๋์,๋ถ์์ด๋ฆ๋ก ์ ๋ชฉ์ ํ์ํ์์ค
์กฐ๊ฑด2 : Seattle ๋๋ Oxford ์์ ๊ทผ๋ฌดํ๋ ์ฌ์
์กฐ๊ฑด3 : ๋์์์ผ๋ก ์ค๋ฆ์ฐจ์์ ๋ ฌํ์์ค
์ฌ์์ด๋ฆ ๋ ์ ๋ถ์์ด๋ฆ
-------------------------
Hall Oxford Sales
[๋ฌธ์ 4] ๋ถ์ID, ์์นID, ๊ตญ๊ฐID๋ฅผ ์ฐ๊ฒฐํด์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์์ค
(๊ด๋ จํ ์ด๋ธ : employees,locations2, departments,countries)
์กฐ๊ฑด1 : ์ฌ์๋ฒํธ,์ฌ์์ด๋ฆ,๋ถ์์ด๋ฆ,๋์,๋์์ฃผ์,๋๋ผ๋ช ๋ก ์ ๋ชฉ์ ํ์ํ์์ค
์กฐ๊ฑด2 : ๋์์ฃผ์์ Ch ๋๋ Sh ๋๋ Rd๊ฐ ํฌํจ๋์ด ์๋ ๋ฐ์ดํฐ๋ง ํ์ํ์์ค
์กฐ๊ฑด3 : ๋๋ผ๋ช , ๋์๋ณ๋ก ์ค๋ฆ์ฐจ์์ ๋ ฌํ์์ค
์กฐ๊ฑด4 : ๋ชจ๋ ์ฌ์์ ํฌํจํ๋ค
