PlayData/MySQL

[Day 15 | MySQL] SubQuery, DML, Table and ๊ณผ์ œ

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 29. 17:59

union all ==> ์ค‘๋ณต๋œ ๊ฐ’๋„ ๊ฐ™์ด ๋ณด์—ฌ์ค€๋‹ค

union ==> ์ค‘๋ณต๋œ ๊ฐ’์€ ์ œ๊ฑฐํ•˜๊ณ  ๋ณด์—ฌ์ค€๋‹ค.

 

 

 

 

subquery : 

์งˆ๋ฌธ์„ ์–ผ๋งˆ๋‚˜ ์งง๊ฒŒ ์งˆ๋ฌธ์„ ์ชผ๊ฐœ๋Š๋ƒ์˜ ์‹ธ์›€. 

์žฅ์ 1. ์•ˆ์—์„œ ์ชผ๊ฐœ์„œ ์งˆ๋ฌธ์„ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด, ๋ฐ์ดํ„ฐ๊ฐ€ ์•ˆ์ „ํ•˜๊ฒŒ ํ˜๋Ÿฌ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์ ๊ฒ€ํ•  ์ˆ˜ ์žˆ์Œ. 

์žฅ์ 2. ์—๋Ÿฌ์œจ์ด ๋–จ์–ด์ง„๋‹ค. 

์žฅ์ 3. ์ ˆ๋Œ€ ๋Šฆ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค. 

 

 

SubQuery01.SQL

-- SubQuery01.sql -- 

smith ์™€ ๊ฐ™์€ job์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„, job์€? 

1) smith ์˜ job์€?


SELECT JOB 
FROM emp 
WHERE (ENAME = 'SMITH');


2) CLERK job์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„, job์€ ?

SELECT ENAME, JOB
FROM EMP 
WHERE (JOB = 'CLERK');

-- 3) ๊ฒฐํ•ฉ 
SELECT ENAME, JOB
FROM EMP 
WHERE (JOB =(SELECT JOB 
			 FROM emp 
		 	 WHERE (ENAME = 'SMITH')));
		 	
		 	
-------------------------------------

-- smith์™€ ๊ฐ™์€ ๊ธ‰์—ฌ ๋“ฑ๊ธ‰์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„,๊ธ‰์—ฌ, ๊ธ‰์—ฌ ๋“ฑ๊ธ‰์€?
-- 1) ์Šค๋ฏธ์Šค ๊ธ‰์—ฌ๋“ฑ๊ธ‰
SELECT s.grade 
from emp e, salgrade s 
where (e.sal BETWEEN s.losal and s.hisal)
and e.ename = 'smith';

-- 2) ๊ทธ ๊ธ‰์—ฌ๋“ฑ๊ธ‰ ์ง์›๋“ค ์ด๋ฆ„, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ๋“ฑ๊ธ‰? 
SELECT e.ename , e.sal , s.grade 
from emp e, salgrade s 
where (s.grade = (SELECT s.grade 
				 from emp e, salgrade s 
				 where (e.sal BETWEEN s.losal and s.hisal)
				 and e.ename = 'smith'));




smith์™€ ๊ฐ™์€ ๋ถ€์„œ์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, ๋ถ€์„œ๋ช…์€?

-- 1) ์Šค๋ฏธ์Šค ๋ถ€์„œ 
SELECT d.dname 
from emp e, dept d 
where e.deptno = d.deptno
and e.ename = 'smith'; 


-- 2)๊ทธ ๋ถ€์„œ ์ง์›๋“ค ์ด๋ฆ„, ๋ถ€์„œ๋ช… 

SELECT e.ename , d.dname 
from emp e , dept d 
where d.dname  = (SELECT d.dname 
				 from emp e, dept d 
				 where e.deptno = d.deptno
				 and e.ename = 'smith'); 
				
-- 2)๊ทธ ๋ถ€์„œ ์ง์›๋“ค ์ด๋ฆ„, ๋ถ€์„œ๋ช…(๊ตณ์ด ์•ˆ๋“œ๋Ÿฌ๋‚˜๋„ ) 
				
SELECT e.ename , d.dname 
from emp e , dept d 
where e.deptno = d.deptno
and (e.deptno  = (SELECT d.deptno 
				  from emp e 
				  where (e.ename = 'smith')));


				 
				 
				 
				 

				 
 -- smith์™€ ๊ฐ™์€ ๋ถ€์„œ์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, job์€?

SELECT e.ename , e.job 
from emp e , dept d 
where (d.dname  = (SELECT d.dname 
				   from emp e, dept d 
				   where e.deptno = d.deptno
		 		   and e.ename = 'smith')); 
				
ํ•ด์„ค: 

SELECT ename, job
from emp 
where (deptno = (select deptno
				 from emp
				 where (ename = 'smith') ) );
				
				

์ „ ์ง์›์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋ณด๋‹ค ๊ธ‰์—ฌ๋ฅผ ๋งŽ์ด ๋ฐ›๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ๋“ฑ๊ธ‰?

-- 1) ์ „ ์ง์› ํ‰๊ท  ๊ธ‰์—ฌ? 
SELECT AVG(e.sal) 
from emp e ;


2) ๊ทธ ๊ฒƒ๋ณด๋‹ค ๋งŽ์ด ๋ฐ›๋Š” ์ง์› ์ด๋ฆ„, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ ๋“ฑ๊ธ‰? 

SELECT e.ename , e.sal , s.grade 
from emp e , salgrade s 
where (e.sal BETWEEN s.losal and s.hisal)
having (e.sal > (SELECT AVG(e.sal) 
				 from emp e));	
				 
------------------------------------------


allen์˜ ์ง์†์ƒ๊ด€๊ณผ ๊ฐ™์€ ์ƒ๊ด€์„ ๋ชจ์‹œ๋Š” ์ง์›๋“ค์˜
์ด๋ฆ„, ์‚ฌ๋ฒˆ, ๋ถ€์„œ๋ช…, ์ง์†์ƒ๊ด€์ด๋ฆ„์€?
(๋‹จ, allen ์ œ์™ธ)


1)allen์˜ ์ง์† ์ƒ๊ด€ ์‚ฌ๋ฒˆ ? -- ๋ฌด์—‡์„ ์„ ํƒํ• ๊ฑด์ง€ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ๋ณด์•„์•ผ  

SELECT mgr 
from emp 
WHERE ename = 'allen';

2) ๊ทธ ์ƒ๊ด€(์‚ฌ๋ฒˆ)์„ ๋ชจ์‹œ๋Š” ์ง์›๋“ค ์ด๋ฆ„,  ์‚ฌ๋ฒˆ, ๋ถ€์„œ๋ช…, ์ง์†์ƒ๊ด€? 

SELECT e1.ename , e1.empno , d.dname , e2.ename 
from emp e1, emp e2, dept d
where (e1.deptno = d.deptno)
and (e1.mgr = e2.empno )
and (e1.mgr = (SELECT mgr 
			   from emp 
			   WHERE ename = 'allen'));
			  
3) Allen ์ œ์™ธ 
SELECT e1.ename , e1.empno , d.dname , e2.ename 
from emp e1, emp e2, dept d
where (e1.deptno = d.deptno)
and (e1.mgr = e2.empno )
and (e1.mgr = (SELECT mgr 
			   from emp 
			   WHERE ename = 'allen'))
and (e1.ename != 'allen');
			  
			  



sales ๋ถ€์„œ ์ง์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ๋งŽ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์›๋“ค์˜
์ด๋ฆ„, ๊ธ‰์—ฌ๋Š”?

1) sales ๋ถ€์„œ ์ง์›๋“ค์˜ ๊ธ‰์—ฌ? 

SELECT e.sal
from dept d , emp e 
where(d.deptno = e.deptno)
and (d.dname = 'sales');


2) ๋งŽ์ด ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„, ๊ธ‰์—ฌ? 

SELECT ename, sal
from emp 
where (sal > x);

3) ๊ฒฐํ•ฉ; 

SELECT ename, sal
from emp e 
where (sal > all (SELECT e.sal	
  	   from dept d, emp e
  	   where ( d.deptno = e.deptno)
  	   and (d.dname = 'sales')));
	


sales ์—…๋ฌด๋ฅผ ํ•˜๋Š” ์ง์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ๋งŽ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„, ๊ธ‰์—ฌ๋Š”? 

1) sales ์—…๋ฌด ์ง์›๋“ค ๊ธ‰์—ฌ

SELECT e.sal  
from emp e 
where job = 'salesman';



SELECT ename, sal
from emp e 
where (sal > all (SELECT e.sal  
				  from emp e 
				  where job = 'salesman'));

1. ๋ฐ์ดํ„ฐ ์ˆ˜์ • ์ž์ฒด๋Š” ์œ„ํ—˜ํ•˜๋‹ค 

2. ์œ„์น˜ ์ˆ˜์ •ํ•  ๋•Œ๋Š”, ์ผ๋‹จ ์ถœ๋ ฅํ•˜๊ณ , ์ˆ˜์ •๋ฌธ์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค (update / set) 

 

 

DML01.SQL

-- DML01.sql --



-- ---------------- INSERT ------------------------ 

SELECT * FROM dept2;

INSERT into dept2 (deptno, dname, loc) values(50, 'IT', 'SEOUL');

INSERT into dept2 ( dname, loc, deptno) values('OP', 'BUSAN', 60); -- ์ปฌ๋Ÿผ๋ช…์„ ์ ์‹œํ•˜๋ฉด ์ˆœ์„œ ๋ฐ”๊ปด๋„ ๊ดœ์ฐฎ์Œ 

INSERT into dept2 values (70, 'AD', 'ILSAN');
INSERT into dept2 values (80, 'MKT', 'Jeju'),
						 (90, 'DEV', 'Jeju');


SELECT * FROM dept2 d ;


desc dept; -- dept์— ์ ํ˜€์žˆ๋Š” ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋ณด์ž 

-- deptno int primary key
-- dname	varchar(20)
-- loc		varchar (20)
-- -----------------------




create table dept3 (deptno INT auto_increment primary key, 
					dname	varchar(20),
					loc		varchar(20));
				
				
insert into dept3 values(null, 'IT', 'SEOUL');
insert into dept3 values(null, 'OP', 'JEJU');
insert into dept3 values(null, 'AD', 'Busan');




alter table dpet3 auto_increment = 100;
insert into dept3 values(null, 'IT', 'SEOUL');
insert into dept3 values(null, 'OP', 'JEJU');
insert into dept3 values(null, 'AD', 'Busan');



set @@auto_increment_increment = 3;  -- 3์”ฉ ์ฆ๊ฐ€ 


insert into dept3 values(null, 'IT', 'SEOUL');
insert into dept3 values(null, 'OP', 'JEJU');
insert into dept3 values(null, 'AD', 'Busan');


SELECT * from dept3;



-------------------------

-- Data ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํ…Œ์ด๋ธ” ๋ณต์‚ฌ 
create table dept4
select  * from dept;


SELECT * from dept4;
------------------------------------------

create table dept5 (deptno int primary key, 
					dname 	varchar(20),
					loc		varchar(20)); 
				
select * from dept5; 

-- ๋น„์–ด์žˆ๋Š” table์— ๋ฐ์ดํ„ฐ๋งŒ ๋ณต์‚ฌ

insert into dept5
select * from dept;

select * from dept5;


-----------------------
-- data ์ˆ˜์ • : update

select * from dept2; 

--> 90๋ฒˆ ๋ถ€์„œ์˜ ์œ„์น˜๋ฅผ LA๋กœ ์ˆ˜์ •. 

1) ์ผ๋‹จ ํ™”๋ฉด์— ์ถœ๋ ฅ์„ ํ•˜๊ณ , 
select loc 
from dept2 
WHERE (deptno = 90);

2) ์ˆ˜์ •๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝ
update dept2 
set loc = 'LA' 
WHERE (deptno = 90);


SELECT * FROM DEPT2;



-- > 70, 90๋ฒˆ ๋ถ€์„œ์˜ ์œ„์น˜๋ฅผ Jeju๋กœ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”. 
SELECT loc 
from dept2
where (deptno = 70 or deptno = 90);

UPDATE dept2
set loc = 'Jeju'
where (deptno in(70, 90));


-- > 80๋ฒˆ ๋ถ€์„œ ์ด๋ฆ„์€ Admin, ์œ„์น˜๋Š” Busan์œผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”.
SELECT d.dname , loc 
from dept2 d 
where d.deptno = 80;

UPDATE dept2 d 
set d.dname = 'Admin',  d.loc = 'Busan' 
where d.deptno = 80;

SELECT * from dept2 d ;


-- ๋ฐ์ดํ„ฐ ์‚ญ์ œ : DELETE 

80๋ฒˆ ๋ถ€์„œ๋ฅผ ์‚ญ์ œ

select * 
from dept2 
where (deptno = 80);

DELETE from dept2 
where (deptno = 80);

SELECT * from dept2;


70๋ฒˆ ๋ถ€์„œ, Jeju ์œ„์น˜์ธ ๋ถ€์„œ๋ฅผ ์‚ญ์ œ : 

SELECT *
from dept2 
where deptno = 70 
and loc = 'Jeju';


select * 
from dept2 
where(deptno, loc) in ((70, 'jeju')); 

DELETE from dept2 
where deptno = 70 
and loc = 'Jeju';


delete 
from dept2 
where(deptno, loc) in ((70, 'jeju')); 


SELECT * from dept2;


-- emp์—์„œ 
-- clerk ์—…๋ฌด๋ฅผ ํ•˜๋ฉด์„œ research ๋ถ€์„œ์ธ ์ง์›๊ณผ 
-- manager ์—…๋ฌด๋ฅผ ํ•˜๋ฉด์„œ accounting ๋ถ€์„œ์ธ ์ง์›๋“ค์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„, job, ๋ถ€์„œ ์ถœ๋ ฅ. 

SELECT e.empno , e.ename , e.job , d.dname 
from emp e , dept d 
where(e.job, d.dname) in (('clerk', 'research'))
or (e.job, d.dname) in (('manager', 'accounting'));

select e.ename, e.empno, e.job, d.dname
from emp e, dept d
where (e.deptno = d.deptno)
 and  (e.job,d.dname) in (('clerk','Research') , ('manager','Accounting'));

 

 

์ˆซ์ž ๋ฐ์ดํ„ฐ ํ˜•์‹: 

๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ํ˜•์‹: 

๊ธฐํƒ€๋ฐ์ดํ„ฐ: ์˜ํ™”, ๋Œ€๋ณธ ๋“ฑ ๋ฐ์ดํ„ฐ๊ฐ€ ์ปฌ๋Ÿผ ํ•˜๋‚˜์— ์ €์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

๋ณ€์ˆ˜๋Š” ๋‚ด์ผ ํ•จ์ˆ˜์ฒ˜๋ฆฌ ํ•  ๋•Œ ๋ชฐ์•„์„œ ํ•  ๊ฒƒ.

 

 

p313

8.1 ํ…Œ์ด๋ธ”

ํ…Œ์ด๋ธ”์˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ์€ ์ง€๊ธˆ๊นŒ์ง€ ๊ณ„์† ํ•ด์™”๋‹ค. ์ฟผ๋ฆฌ๋กœ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด๋Š” ๊ฒƒ์ด ์ค‘์š”. 

p313 ์ƒ˜ํ”Œ ์˜ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์ด์•ผ๊ธฐ ํ•ด๋ณด์ž. 

๋งŒ์•ฝ ์‡ผํ•‘๋ชฐ์„ ๋งŒ๋“ ๋‹ค. 

 

๊ทธ๋Ÿฌ๋ฉด

1. ํšŒ์› ํ…Œ์ด๋ธ” 

2. ๊ตฌ๋งค ํ…Œ์ด๋ธ”

์ด๊ฑด ์‰ฝ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๊ณ , 

์ •์‹์ ์œผ๋กœ ๋งŒ๋“ค๋ ค๋ฉด ๋ชจ๋ธ๋ง์ด๋ผ๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•จ. 

 

ํ…Œ์ด๋ธ” ๋งŒ๋“ค๋•Œ,

์ด๋ฆ„๋ช… ๋ฌด์—‡์œผ๋กœ ํ• ์ง€, ์ปฌ๋Ÿผ๋ช… ๋ฌด์—‡์œผ๋กœ ํ• ์ง€, ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ฌด์—‡์œผ๋กœ ํ• ์ง€, Null๊ฐ’ ์ฒ˜๋ฆฌ ํ• ์ง€๋ง์ง€ 

 

 

์–ธ์–ด๋ณด๋‹ค ๋” ๋น ๋ฅด๊ฒŒ ํ•„์š”ํ•œ๊ฑด DB ์„ค๊ณ„์„œ(์ •์˜์„œ) 

๊ทธ ๋‹ค์Œ์— ๋“ค์–ด๊ฐˆ ๊ฒƒ์€ ์ œ์•ฝ์กฐ๊ฑด. 

 

์ œ์•ฝ์กฐ๊ฑด: 

DBํ™”ํ•œ๋‹ค๋Š” ๊ฒƒ์€: DBMS (DB Management system)

- ์ฃผ Data (ํ–‰ ๋‹จ์œ„ Record)

- DB Object(Table ๊ด€๋ฆฌ) 

- ํ†ต์‹ (TCP/IP) --> ๋ˆ„๊ตฌ๋‚˜ ๋™์‹œ์—

- ๋ณด์•ˆ(์ธ์ฆ/์ธ๊ฐ€) 

- ๊ด€๋ฆฌ(๋ฐฑ์—…/๋ณต์›, ์ด์ค‘ํ™”) 

 

Table ์ƒ์„ฑํ•  ๋•Œ, ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์ง€์ผœ์•ผ ํ•œ๋‹ค.: <-- ๋ฐ์ดํ„ฐ์— ์ด์ƒํ•œ ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š๋„๋ก ๋ฐฉ์–ดํ•ด์ฃผ์–ด์•ผ

1. DataType - ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๋ฌด๊ฒฐ์„ฑ์„ ์ง€์ผœ์•ผ (๋‚˜์ด์— a๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ๋จ, ์ƒ์‹์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ)

2. ์ œ์•ฝ์กฐ๊ฑด(Constraint) <-- ๋‚˜์ด์— ์Œ์ˆ˜, 1000์‚ด ๋“ฑ์ด ์žˆ์œผ๋ฉด ์•ˆ๋จ 

3. ์ฐธ์กฐ <-- ๊ฐ€์ž…ํ•˜์ง€ ์•Š์€ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋ฌผ๊ฑด์„ ์‚ฐ ํ”์ ? ์•ˆ๋จ. 

 

์ œ์•ฝ์กฐ๊ฑด(Constraint)  :  5 + @๊ฐœ 

  • PK (Primary Key) : ๊ธฐ๋ณธํ‚ค ์ œ์•ฝ์กฐ๊ฑด ==> Unique + Null( X ) --> ํ…Œ์ด๋ธ” ๋‹น ํ•˜๋‚˜ 
  • UK(Unique) :์œ ๋‹ˆํฌ ์ œ์•ฝ์กฐ๊ฑด           ==> Unique + Null( O ) --> ํ…Œ์ด๋ธ” ๋‹น n
  • FK: ์™ธ๋ž˜ํ‚ค                                         ==> ์ž๊ธฐ/๋‹ค๋ฅธ table unique ํ•œ ์ปฌ๋Ÿผ ์ฐธ์กฐ
  • CK(Check) :                                     ==> ๋ฒ”์œ„(age between 1 and 120) , ์—ด๊ฑฐ ( gender in ('M', 'F')
  • NN : Not Null                                   ==> Null ํ—ˆ์šฉ ์—ฌ๋ถ€ 
  • DF(Default) : ๊ธฐ๋ณธ๊ฐ’์„ ์ •์˜             ==> ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’์„ ์ž…๋ ฅ

์ œ์•ฝ์กฐ๊ฑด๋„ ์ด๋ฆ„์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. (์•„๋ž˜ Mob1 ์ฒ˜๋Ÿผ) 

 

 

 

 

๊ฐ•์‚ฌ๋‹˜ ๊ฐ ์Šฌ๋กœ ์ „์†กํ•˜๊ธฐ ! 

 

๊ณผ์ œ 1. 

์ œ์ถœ ์‹œ ๋„ค์ด๋ฐ:  [๊ณผ์ œ]_2021.11.29_ํ™๊ธธ๋™_(selectํ€ด์ฆˆ) 

-- Q. Martin์˜ job ์†Œ์† ์ง์›๋“ค์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋ณด๋‹ค ๊ธ‰์—ฌ๋ฅผ ๋‚ฎ๊ฒŒ ๋ฐ›๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„,job, ๋ถ€์„œ๋ช…, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ๋“ฑ๊ธ‰, ์ƒ๊ด€์ด๋ฆ„, ์ƒ๊ด€๋ถ€์„œ๋ช…์€?
โ€‹
-- 1) martin์€ ๋ฌด์ŠจJOB?  --> salesman
โ€‹
SELECT e1.job
from emp e1
where (e1.ename ='Martin');
โ€‹
โ€‹
โ€‹
-- 2)ํ•ด๋‹น job์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋Š”? --> 1400
โ€‹
SELECT AVG(e1.sal) 
from emp e1 
where e1.job = (SELECT e1.job
				from emp e1
				where (e1.ename ='Martin'));
โ€‹
โ€‹
-- 3) ๊ทธ ๊ธ‰์—ฌ๋ณด๋‹ค ๋‚ฎ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๊ณ  ์žˆ๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„,job, ๋ถ€์„œ๋ช…, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ๋“ฑ๊ธ‰, ์ƒ๊ด€์ด๋ฆ„, ์ƒ๊ด€๋ถ€์„œ๋ช…์€? (e1. ๋ถ€ํ•˜ , e2. ๋งค๋‹ˆ์ €)
โ€‹
SELECT e1.ename , d1.dname , e1.sal, s1.grade , e2.ename , d2.dname 
from emp e1, dept d1, salgrade s1, emp e2, dept d2
where (e1.mgr = e2.empno)
and (e1.sal BETWEEN s1.losal  and s1.hisal )
and ( e1.deptno = d1.deptno )
and ( e2.deptno = d2.deptno )
and (e1.sal  < (SELECT AVG(e1.sal) 
				from emp e1 
				where e1.job = (SELECT e1.job
								from emp e1
								where (e1.ename ='Martin'))));

๊ณผ์ œ 2
์ œ์ถœ ์‹œ ๋„ค์ด๋ฐ:  [๊ณผ์ œ]_2021.11.29_ํ™๊ธธ๋™_(Table ์„ค๊ณ„) 

 

ํ…Œ์ด๋ธ” ์„ค๊ณ„. ์›น ๊ฒŒ์‹œํŒ๊นŒ์ง€ ์—ฐ๊ฒฐ

ํ…Œ์ด๋ธ” ์„ค๊ณ„๋„๋ฅผ ์—‘์…€๋กœ ๋งŒ๋“ค๊ธฐ (์œ„ ์˜ˆ์‹œ ๋Š๋‚Œ์ฒ˜๋Ÿผ) 

ํšŒ์› Table

๊ฒŒ์‹œํŒ Table

 

๋ญ˜ ๊ต‰์žฅํžˆ ๋งŽ์ด ํ–ˆ๋Š”๋ฐ ๋ณต์Šต์„ ์ข€ ํ•ด์•ผ๊ฒ ๋‹ค 

(๏ฝก•ฬ๏ธฟ•ฬ€๏ฝก) 

 

์Šคํ์—˜์˜ ์‹ ์ด ๋˜์ž ๐Ÿ‘ผ๐Ÿป