JOIN ๋ฌธ ์ฌ์ฉํ๋ ๋ฒ
๋ค์ํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๋ ์ค์ต์ด ํ์ํด์!
์ธ์ด๋ก ์ดํดํ๊ณ --> ๊ทธ๋ค์์ ์ง๊ธฐ.
์ฐ์ ์๊ฐ์์ด select from ์ฐ๊ณ , ๊ทธ ํ ์ฐ๊ฒฐ ๋ฐ์ดํฐ ์ฐพ์ ์จ์ฃผ๊ณ ์ด๋ฐ ์์ผ๋ก!
๋์ค์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ค๊ณํ ๋ ์ ์ ๋ฅ๋ ฅ์ ๊ณ ๊ฐ์ ์๊ตฌ๋ฅผ ํ๊ธ๋ก ํํํ ์ ์๋ค! ์ด๊ฒ ์ ์ผ ์ค์ํด์ค!!
Q. KING ์ด๋ฆ, ๋ถ์๋ช , ๋ถํ์ง์ ์ด๋ฆ, ๋ถ์๋ช ?
์ด๋ ๊ฒ ๊ตฌ์กฐ์ก๊ธฐ
Q. 4 ๊ธ์ฌ๋ฑ๊ธ์ ํด๋นํ๋ ์ง์๋ค์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช , ๊ธ์ฌ๋ฑ๊ธ๊ณผ ๊ทธ ์ง์๋ค์ ๋ถํ์ง์์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช , ๊ธ์ฌ๋ฑ๊ธ์?
- m์ ๋งค๋์
- e๋ ๋ถํ์ง์
์ด๋ฐ ๊ตฌ์กฐ๋ก ์๊ฐํด์ ํ์ด๊ฐ๊ธฐ
Join01.SQL
-- Join01.sql --
-- SMITH์ ์ด๋ฆ, ์์ ๋ถ์ ์ด๋ฆ์?
SELECT ename, dname
FROM emp, dept
WHERE (emp.deptno = dept.deptno)
AND (ename = 'SMITH');
-- research ๋ถ์ ์์ ์ง์๋ค์ ๋ถ์๋ช
, ์์น, ์ง์์ด๋ฆ, ์ฌ๋ฒ์?
select dname, loc, ename, empno
from emp, dept
where (emp.deptno = dept.deptno)
and (dname = 'research');
-- sales ๋ถ์์ ์ง์๋ค์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์์ด๋ฆ์?
SELECT ename, empno, dname
from emp, dept
where (emp.deptno = dept.deptno)
and (dname = 'sales');
-- King์ ์ด๋ฆ, ์ฌ๋ฒ, job, ๋ถ์๋ช
, ์์น๋ ?
select ename, empno, job, dname, loc
from emp, dept
where (emp.deptno = dept.deptno)
and ename = 'king';
-- smith์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช
, ๋ถ์๋ฒํธ๋ ? deptno ์ด๋ค ๊ฒ์ ์จ์ผํ๋์ง ์ ํด์ค์ผ
SELECT emp.ename, emp.empno, dept.dname, emp.deptno
from emp , dept
where (emp.deptno = dept.deptno)
AND emp.ename = 'SMITH';
-- smith์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช
, ๋ถ์๋ฒํธ๋ ? ๋ณ์นญ ์ฃผ๊ธฐ
SELECT e.ename, e.empno, d.dname, e.deptno
from emp e, dept d
where (e.deptno = d.deptno)
AND e.ename = 'SMITH';
-- ๋ถ์ ์ด๋ฆ๋ณ ์ธ์์? --'๋ณ' ๋ถ์ผ๋ฉด ์ ๋ ฌ ์๋๋ฉด ์ํ
SELECT d.dname, count(e.empno)
from emp e, dept d
where ( d.deptno = e.deptno ) -- ๋๋๋๋ ๋ค๋ฆฌ ๊ทธ๋๋ก ์จ์ฃผ๋ฉด ๋จ , ๋์ค์ ์กฐ์ธ๋ฌธ์ ๋
ธ๊ฐ๋ค
group by d.dname; -- ๊ทธ๋ฃจํํ๋ฉด ๋ฌด์์ผ๋ก ๊ทธ๋ฃจํํ์ง select ๋ค์ ์จ์ค๋ค.
-- ๋ถ์ ์ด๋ฆ๋ณ ํ๊ท ๊ธ์ฌ?
SELECT d.dname, AVG(sal)
from emp e, dept d
where ( d.deptno = e.deptno ) -- ๋๋๋๋ ๋ค๋ฆฌ ๊ทธ๋๋ก ์จ์ฃผ๋ฉด ๋จ , ๋์ค์ ์กฐ์ธ๋ฌธ์ ๋
ธ๊ฐ๋ค
group by d.dname;
-- ๋ถ์๋ณ ํ๊ท ๊ธ์ฌ๊ฐ 2000์ด ๋๋ ๋ถ์์ ํ๊ท ๊ธ์ฌ?
SELECT d.dname, avg(e.sal)
from emp e, dept d
where ( d.deptno = e.deptno )
group by d.dname
having (avg(e.sal)>=2000) ;
-- ์ค๋ฏธ์ค์จ์ ์ด๋ฆ, ๊ธ์ฌ, ๊ธ range๋ ์ด๋์ ์์๊น?
SELECT e.ename , e.sal, s.grade
from emp e, salgrade s
where (e.sal between s.losal AND s.hisal)
and (e.ename = 'SMITH') ;
-- 3 ๊ธ์ฌ๋ฑ๊ธ์ ๊ธ์ฌ๋ฑ๊ธ, ํด๋น ์ง์์ ์ด๋ฆ, ๊ธ์ฌ๋?
SELECT s.grade , e.ename , e.sal
from emp e, salgrade s
where (s.grade = 3);
-- 1,4 ๊ธ์ฌ๋ฑ๊ธ์ ๊ธ์ฌ๋ฑ๊ธ, ํด๋น ์ง์์ ์ด๋ฆ, ๊ธ์ฌ๋?
SELECT s.grade, e.ename, e.sal
from emp e, salgrade s
where (s.grade = 1 or s.grade = 4)
order by s.grade ;
-- ๊ธ์ฌ๋ฑ๊ธ๋ณ ์ธ์์๋?
select s.grade, count(empno)
from emp e, salgrade s
where (e.sal BETWEEN s.losal and s.hisal)
group by s.grade;
-- sales ๋ถ์์ ๋ถ์๋ช
, ์์์ง์์ด๋ฆ, ์ฌ๋ฒ, ๊ธ์ฌ, ๊ธ์ฌ๋ฑ๊ธ์?
select d.dname, e.ename, e.empno, e.sal, s.grade
from emp e, dept d , salgrade s
where (e.sal BETWEEN s.losal and s.hisal)
and d.dname = 'sales';
-- research ๋ถ์ ์์ ์ง์๋ค์ ๋ถ์๋ช
, ๋ถ์๋ฒํธ, ์ด๋ฆ, ์ฌ๋ฒ, ๊ธ์ฌ, ๊ธ์ฌ ๋ฑ๊ธ์?
SELECT d.dname , d.deptno , e.ename, e.empno , e.sal , s.grade
from emp e , dept d , salgrade s
WHERE (e.sal BETWEEN s.losal and s.hisal)
and d.dname = 'research';
-- KING์ ์ด๋ฆ, ์ฌ๋ฒ, ๊ธ์ฌ, ๊ธ์ฌ๋ฑ๊ธ, ๋ถ์๋ช
์?
SELECT e.ename ,e.empno ,e.sal , s.grade ,d.dname
from emp e , dept d , salgrade s
where e.deptno = d.deptno
and (e.sal BETWEEN s.losal and s.hisal )
and (e.ename = 'king');
-- smith์ ์ด๋ฆ๊ณผ ์ง์์๊ด์ ์ด๋ฆ์?
SELECT a1.ename , a2.ename as 'manager'
from emp a1, emp a2
where a1.ename = 'smith'
and a2.empno = a1.mgr ;
-- ford์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถํ์ง์์ ์ด๋ฆ, ์ฌ๋ฒ์?
SELECT a1.ename , a1.empno , a2.ename as '๋ถํ์ง์', a2.empno
from emp a1, emp a2
WHERE a1.ename = 'ford'
and a1.empno = a2.mgr ;
-- King ์ ์ด๋ฆ, ๋ถ์๋ช
, ๋ถํ์ง์์ ์ด๋ฆ, ๋ถ์๋ช
์?
-- ํ
์ด๋ธ 4๊ฐ์ด๋ฏ๋ก ์ต์ join์กฐ๊ฑด 3๊ฐ.
SELECT e1.ename , d1.dname , e2.ename , d2.dname
from emp e1, dept d1, emp e2, dept d2
where e1.deptno = d1.deptno
and e2.deptno = d2.deptno
and e1.empno = e2.mgr
and e1.ename = 'king';
-- ์ dept ์ด์ฐจํผ ๊ฐ์ ๋
ผ๋ฆฌ๋ก ์ฐ๊ฒฐํ๋๋ฐ ํ
์ด๋ธ ๊ผญ ๋๋ ์ ์จ์ผํ๋์?
-- ์๋๋ ์ฐ๋ฉด ์ธ ํ
์ด๋ธ์ ๋์ ๋ง์กฑํ๋ ๊ฒฝ์ฐ๋ง ๋์ค๊ธฐ ๋๋ฌธ์
๋๋ค
select me.ename,dd.dname , ee.ename, dd.dname
from emp me, dept dd, emp ee
where ( me.deptno = dd.deptno )
and ( me.empno = ee.mgr )
and ( ee.deptno = dd.deptno )
and (me.ename = 'King');
-- 4 ๊ธ์ฌ๋ฑ๊ธ์ ํด๋นํ๋ ์ง์๋ค์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช
, ๊ธ์ฌ๋ฑ๊ธ๊ณผ
-- ๊ทธ ์ง์๋ค์ ๋ถํ์ง์์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถ์๋ช
, ๊ธ์ฌ๋ฑ๊ธ์?
SELECT e1.ename , e1.empno , d1.dname , s1.grade ,
e2.ename , e2.empno ,d2.dname , s2.grade
from emp e1, salgrade s1, dept d1,
emp e2, salgrade s2, dept d2
where e1.deptno = d1.deptno
and e2.deptno = d2.deptno
and e1.empno = e2.mgr
and e1.sal BETWEEN s1.losal and s1.hisal
and e2.sal BETWEEN s2.losal and s2.hisal
and s1.grade = 4;
-- --------------------------------------------
drop table dept2; -- ํ
์ด๋ธ ์ญ์
-- dept์ ์๋ ๋ชจ๋ ์ปฌ๋ผ์ dept2์ ๋ณต์ฌํด๋ผ
create table dept2
select * from dept;
drop table emp2;
-- k๊ฐ ๋ค์ด์๊ฑฐ๋ s๋ก ๋๋๋ ์ ๋ค๋ง ๋ณต์ฌํด๋ผ
create table emp2
select ename,empno,deptno
from emp
where ename like '%K%' or ename like '%S';
select * from emp2;
select * from dept2;
-- ๋ฐ์ดํฐ ์
๋ ฅ
INSERT into emp2 values('Tom', 7777, null);
-- emp2, dept2
-- ์ ์ง์์ ์ด๋ฆ, ์ฌ๋ฒ, ์์น, ๋ถ์๋ฒํธ?
SELECT *
from emp2 e ,dept2 d
where e.deptno = d.deptno ;
-- tom? operation ๋ถ์? ์ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ ์์ด์ ์๋์ด
-- ANSI JOIN 273p
select *
from emp2 e inner join dept2 d on(e.deptno = d.deptno);
SELECT *
from emp2 e left outer join dept2 d on (e.deptno = d.deptno);
SELECT *
from emp2 e right outer join dept2 d on (e.deptno = d.deptno);
-- mysql์์๋ full outer join ์ด ์๋จ.
SELECT *
from emp2 e full outer join dept2 d on (e.deptno = d.deptno);
-- ๋ฐ๋ผ์ ๋ฐ๋ก ๋ฐ๋ก ๊ตฌํ๊ฑธ ํฉ์ณ์ผ
SELECT *
from emp2 e left outer join dept2 d on (e.deptno = d.deptno)
union
SELECT *
from emp2 e right outer join dept2 d on (e.deptno = d.deptno);
join ์ ํํ๋ ๋ ๊ฐ์ง๊ฐ ์๋ค.
1. ์ผ๋ฐ join : where ์กฐ๊ฑด // ๊ฐ๋ฐ์๋ค์ ์ด๊ฑธ ์ ํธํ๊ณ
2. ANSI join : from ํ ์ด๋ธ ( ) join ํ ์ด๋ธ on ( join ์กฐ๊ฑด ) // ์์ฆ ํ๋ก๊ทธ๋จ๋ฑ ์ํ์ ์ด๊ฑธ๋ก ์ด๋ค
๊ณ ๋ก ๋ ๋ค ์์์ผ!
Join02.sql
-- JOIN02 --
-- tom? operation ๋ถ์? ์ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ ์์ด์ ์๋์ด
-- ANSI JOIN 273p
select *
from emp2 e inner join dept2 d on(e.deptno = d.deptno);
SELECT *
from emp2 e left outer join dept2 d on (e.deptno = d.deptno);
SELECT *
from emp2 e right outer join dept2 d on (e.deptno = d.deptno);
-- mysql์์๋ full outer join ์ด ์๋จ.
SELECT *
from emp2 e full outer join dept2 d on (e.deptno = d.deptno);
-- ๋ฐ๋ผ์ ๋ฐ๋ก ๋ฐ๋ก ๊ตฌํ๊ฑธ ํฉ์ณ์ผ
SELECT *
from emp2 e left outer join dept2 d on (e.deptno = d.deptno)
union
SELECT *
from emp2 e right outer join dept2 d on (e.deptno = d.deptno);
-----------------------------------
-- SMITH์ ์ด๋ฆ, ์์ ๋ถ์ ์ด๋ฆ์?
SELECT ename, dname
FROM emp, dept
WHERE (emp.deptno = dept.deptno)
AND (ename = 'SMITH');
SELECT e.ename , d.dname
from emp e inner join dept d
on (e.deptno = d.deptno)
WHERE (e.ename = 'smith');
----------------------------------------------------
-- RESEARCH ๋ถ์ ์์ ์ง์๋ค์ ๋ถ์๋ช
, ์์น, ์ด๋ฆ, ์ฌ๋ฒ์?
SELECT d.dname , d.loc , e.ename , e.empno
from emp e inner join dept d
on (e.deptno = d.deptno)
where (d.dname= 'research');
-- ๋ถ์๋ช
๋ณ ํ๊ท ๊ธ์ฌ๋?
SELECT d.dname , AVG(e.sal)
from dept d inner join emp e
on (e.deptno = d.deptno)
group by d.dname ;
-- smith์ ์ด๋ฆ, ์ฌ๋ฒ, ๊ธ์ฌ, ๊ธ์ฌ๋ฑ๊ธ์?
SELECT e.ename , e.empno , e.sal , s.grade
from emp e inner join dept d on (e.deptno = d.deptno)
inner join salgrade s on (e.sal BETWEEN s.losal and s.hisal)
where e.ename = 'smith';
-- 3๊ธ์ฌ๋ฑ๊ธ์ ๋ฑ๊ธ, ์ง์์ด๋ฆ, ๊ธ์ฌ๋?
SELECT s.grade , e.ename , e.sal
FROM emp e
inner join salgrade s
on(e.sal BETWEEN s.losal and s.hisal)
where s.grade = 3;
-- ๊ธ์ฌ๋ฑ๊ธ๋ณ ์ง์์๋?
select s.grade , count(e.empno)
from emp e inner join salgrade s
on (e.sal BETWEEN s.losal and s.hisal)
group by s.grade ;
-- King์ ์ด๋ฆ, ์ฌ๋ฒ, ๋ถํ์ง์๋ค์ ์ด๋ฆ, ์ฌ๋ฒ์?
SELECT me.ename , me.empno , ee.ename , ee.empno
from emp me inner join emp ee
on (me.empno = ee.mgr)
where (me.ename = 'king')
3 ๊ธ์ฌ๋ฑ๊ธ์ ๊ธ์ฌ๋ฑ๊ธ, ํด๋น ์ง์์ ์ด๋ฆ, ๊ธ์ฌ, ๋ถ์๋ช
์ ?
select s.grade , e.ename , e.sal, d.dname
from salgrade s inner join emp e on(e.sal between s.losal and s.hisal)
inner join dept d on(d.deptno = e.deptno)
where (s.grade = 3);
---------------------------------
sales ๋ถ์์ ๋ถ์๋ช
, ์์์ง์์ด๋ฆ, ์ฌ๋ฒ, ๊ธ์ฌ, ๊ธ์ฌ๋ฑ๊ธ์?;
SELECT d.dname , e.ename ,e.empno ,e.sal ,s.grade
from emp e inner join salgrade s on (e.sal BETWEEN s.losal and s.hisal)
inner join dept d ON (e.deptno = d.deptno)
where d.dname = 'sales';
King ์ ์ด๋ฆ, ๋ถ์๋ช
, ๋ถํ์ง์์ ์ด๋ฆ, ๋ถ์๋ช
์?;
-- ๋ด ํ
SELECT e1.ename , d1.dname , e2.ename , d2.dname
from emp e1 inner join dept d1 on (e1.deptno = d1.deptno)
inner join emp e2 on (e1.empno = e2.mgr)
inner join dept d2 on (e2.empno = d2.deptno)
where e1.ename = 'king';
-- ํด์ค
select me.ename, md.dname, ee.ename, ed.dname
from emp me inner join dept md on (me.deptno = md.deptno)
inner join emp ee on (me.empno = ee.mgr)
inner join dept ed on (ee.deptno = ed.deptno)
where (me.ename = 'king');
King ์ ์ด๋ฆ๊ณผ ๋ถํ์ง์๋ค์ ์ด๋ฆ,job,๋ถ์๋ช
,๊ธ์ฌ,๊ธ์ฌ๋ฑ๊ธ์?;
SELECT e1.ename , e2.ename , e2.job , d2.dname , e2.sal , s2.grade
from emp e1 inner join emp e2 on (e1.empno = e2.mgr)
inner join dept d2 on (e2.deptno = d2.deptno )
inner join salgrade s2 on (e2.sal BETWEEN s2.losal and s2.hisal)
where e1.ename = 'king';
-- ํด์ค
King ์ ์ด๋ฆ๊ณผ ๋ถํ์ง์๋ค์ ์ด๋ฆ,job,๋ถ์๋ช
,๊ธ์ฌ,๊ธ์ฌ๋ฑ๊ธ์?;
select me.ename, ee.ename, ee.job, ed.dname, ee.sal , es.grade
from emp me inner join emp ee on (me.empno = ee.mgr)
inner join dept ed on (ee.deptno = ed.deptno)
inner join salgrade es on (ee.sal between es.losal and es.hisal)
where (me.ename = 'king');
-- emp2 , dept2
-- outerjoin
๋ชจ๋ ๋ถ์์ ์ง์ ์๋?
SELECT d.dname, COUNT(e.empno)
from dept2 d left outer join emp2 e
on (d.deptno = e.deptno)
group by d.dname ;
SELECT d.dname, count(e.empno)
from emp2 e right outer join dept2 d
on(d.deptno = e.empno)
group by d.dname ;
-- cross join --(๋ชจ๋ ๊ฒฝ์ฐ์ ์) 288~289p
select *
from emp2 e, dept2 d;
SELECT *
from emp2 e cross join dept2 d;
-- selfjoin p290
๋ค์์ฃผ๋ถํฐ ๊ณผ์ ๋ฅผ ๋ด์ค์ง๋ ๋ชฐ๋ผ์
๋ ฬฬ์ฉฬฬ
๐คฆ๐ปโ๏ธ
'PlayData > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day 18 | MySQL] DAO, VO๋ฑ (์ดํด ing) (2) | 2021.12.02 |
---|---|
[Day 16 | MySQL] Table, Variable, ๋ฐ์ดํฐ ๋ชจ๋ธ๋ง, (0) | 2021.12.01 |
[Day 15 | MySQL] SubQuery, DML, Table and ๊ณผ์ (0) | 2021.11.29 |
[Day13 | MySQL] SQL ๊ธฐ๋ณธ RDB, where, groupby, having (0) | 2021.11.25 |
[Day12 | MySQL] MySQL, Workbench, ํ๊ฒฝ๋ณ์ ์ค์ , select, order by, distinct, ๋ณ๋ช (0) | 2021.11.24 |