PlayData/MySQL

[Day13 | MySQL] SQL ๊ธฐ๋ณธ RDB, where, groupby, having

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 25. 18:00

 

์•„๋ฌด ์ƒ๊ฐ์—†์ด select, from(์–ด๋””์„œ ์˜ค๋Š”์ง€) ์„ ๋จผ์ € ์น˜๊ณ , 

์‹ค์ œ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ๊ณ ๋ฏผํ•˜๊ณ , 

์ปฌ๋Ÿผํ˜• ๋ช…์‚ฌ๋ฅผ ๊ณ ๋ฏผํ•˜๊ณ , 

 

Select

1) ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜? --> Sub Query

2) Table์˜ ๊ฐœ์ˆ˜? --> Join (N-1๊ฐœ) 

3) ์กฐ๊ฑด์˜ ๊ฐœ์ˆ˜, ์œ„์น˜, ์œ ํ˜•

- 1) ์ผ๋ฐ˜ ์กฐ๊ฑด : from / where (where) <== Table์˜ column

- 2) ๊ทธ๋ฃน ์กฐ๊ฑด: group by / having (์กฐ๊ฑด) <== ์ง‘๊ณ„ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๊ฐ’ 

 

์œ ํ˜•

1. Join ์กฐ๊ฑด

2. pont ์กฐ๊ฑด : ๋”ฑ ์ด ๊ฐ’์ด๋‹ค. 

3. Range ์กฐ๊ฑด: ๋ฒ”์œ„ ์กฐ๊ฑด (>, <, =, between and ๋“ฑ ํ‘œํ˜„ ๋งŽ์ด ์‚ฌ์šฉ)

4. ์—ด๊ฑฐ ์กฐ๊ฑด: in(  ,  ,  )

5. ์ƒํƒœ ์กฐ๊ฑด: is null

6. ํŒจํ„ด ์กฐ๊ฑด: like _ (ํ•œ์ž๋ฆฌ ์•„๋ฌด๊ฑฐ๋‚˜), % (์—ฌ๋Ÿฌ์ž๋ฆฌ ์•„๋ฌด๊ฑฐ๋‚˜) ์ด๋ฆ„์ด a๋กœ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ๋žŒ์„ ๊ณจ๋ผ์ฃผ์„ธ์š” 

and / or / not ์กฐ๊ฑด $

 

์ƒˆ ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๋ฉด ์ด๊ฑฐ ๋ฐ”๊ฟ”์ค˜์•ผ! 

 

 

Where01.sql

USE empdb;

SELECT ename
FROM emp
WHERE(ename = 'smith')

-- clerk ์—…๋ฌด๋ฅผ ํ•˜๋Š” ์ง์›๋“ค์˜ ์ด๋ฆ„, ์—…๋ฌด, ์‚ฌ๋ฒˆ์€?
SELECT ename, job, enpno
FROM emp
WHERE (job = 'clerk');

-- ๊ธ‰์—ฌ๊ฐ€ 3000 ์ด์ƒ์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, ์‚ฌ๋ฒˆ, ๊ธ‰์—ฌ๋Š”?

SELECT ename, empno, sal
FROM emp
WHERE (sal >=3000);



-- ๊ธ‰์—ฌ๊ฐ€ 1550~3000์‚ฌ์ด์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, ์‚ฌ๋ฒˆ, ๊ธ‰์—ฌ๋Š”? 

SELECT ename, empno, sal
FROM emp
WHERE(sal between 1550 and 3000);



-- 30๋ฒˆ ๋ถ€์„œ์—์„œ salesman์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, ๋ถ€์„œ๋ฒˆํ˜ธ, job์€?
SELECT ename, deptno, job
FROM emp
WHERE (deptno = 30)
and (job = 'salesman');


-- ๊ธ‰์—ฌ๊ฐ€ 1200 ~ 3000 ์‚ฌ์ด์˜ ์ง์›๋“ค ์ค‘ ์—…๋ฌด๊ฐ€ salesman ์ธ 
-- ์ง์›๋“ค์˜ ์ด๋ฆ„, ์‚ฌ๋ฒˆ, ์—…๋ฌด, ๊ธ‰์—ฌ๋Š”? 
select ename, empno, job, sal
from emp 
where (sal between 1200 and 3000)
and (job = 'salesman');


-- ๊ธ‰์—ฌ๊ฐ€ 1250, 1600, 3000 ์ธ ์ง์›๋“ค ์ค‘ ์—…๋ฌด๊ฐ€ analyst์ธ ์ง์›๋“ค์˜ ์ด๋ฆ„, ์‚ฌ๋ฒˆ, ์—…๋ฌด, ๊ธ‰์—ฌ๋Š”? 
-- ๋ฐฉ๋ฒ• 1. 
select ename, empno, job, sal
from emp 
where (sal = 1250 or sal = 1600 or sal = 3000)
and (job = 'analyst');

-- ๋ฐฉ๋ฒ• 2. 
select ename, empno, job, sal
from emp 
where sal in (1250, 1600, 3000)
and (job = 'analyst');


-- S๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„ ์‚ฌ๋ฒˆ, ๊ธ‰์—ฌ๋Š”? 
select ename, empno, sal
from emp 
where(ename like 's%') ; -- % ์—ฌ๋Ÿฌ์ž๋ฆฌ ์•„๋ฌด๊ฑฐ๋‚˜, _ ํ•œ ์ž๋ฆฌ ์•„๋ฌด๊ฑฐ๋‚˜ 

-- ์•ž์—์„œ ๋‘ ๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ A๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„ ์‚ฌ๋ฒˆ? 
select ename, empno, sal
from emp
where (ename like '_A%');

-- ๋’ค์—์„œ ๋‘ ๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ A๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ์ง์›๋“ค์˜ ์ด๋ฆ„ ์‚ฌ๋ฒˆ?
select ename, empno, sal
from emp
where (ename like '%A_');

SQL ์žฌ๋ฐŒ๋‹ค!!!!

 

 

null๊ฐ’์žˆ์œผ๋ฉด ๊ณ„์‚ฐ ๊ฐ’ ์ œ๋Œ€๋กœ ์•ˆ๋‚˜์˜ค๋‹ˆ๊นŒ, 

๋ช…์‚ฌ ๊ฐ’์ด null์ด๋ฉด 0์œผ๋กœ ๋Œ€์ฒดํ•˜์ž => ifnull( ๋ช…์‚ฌ, 0) 

 

 

GroupByHaving01.sql

-- GroupByHaving01.sql -- 

-- ๋ชจ๋“  ์ง์›๋“ค์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„, ๊ธ‰์—ฌ ํ‰๊ท , ์ตœ๋Œ€ ๊ธ‰์—ฌ, ์ตœ์†Œ ๊ธ‰์—ฌ;

SELECT sum(sal), avg(sal), max(sal), min(sal) -- ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์ง‘๊ณ„(๊ทธ๋ฃน)ํ•จ์ˆ˜ 
from emp;


-- ์—…๋ฌด ๋ณ„ ์ง์›๋“ค์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„, ๊ธ‰์—ฌ ํ‰๊ท , ์ตœ๋Œ€ ๊ธ‰์—ฌ, ์ตœ์†Œ ๊ธ‰์—ฌ;
SELECT job, sum(sal), avg(sal), max(sal), min(sal) -- ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์ง‘๊ณ„(๊ทธ๋ฃน)ํ•จ์ˆ˜ 
from emp
group by job; -- ๊ทธ๋ฃน๋ฐ”์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์žฅ ์•ž์— ์จ์ค€๋‹ค. ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ! 


-- ๋ถ€์„œ๋ฒˆํ˜ธ ๋ณ„ ํ‰๊ท ๊ธ‰์—ฌ? 
select deptno, avg(sal)
from emp 
group by deptno ;


-- ์—…๋ฌด๋ณ„ ํ‰๊ท  ๊ธ‰์—ฌ๊ฐ€ 3000 ์ด์ƒ์ธ ์—…๋ฌด์™€ ํ‰๊ท ์€? 
select job, AVG(sal)
from emp 
-- WHERE ( ํ‰๊ท  ๊ธ‰์—ฌ๊ฐ€ 3000์ด์ƒ? <-- ํ…Œ์ด๋ธ”์—๋Š” ํ‰๊ท ๊ฐ’ ์•ˆ๋‚˜์™€์žˆ์œผ๋ฏ€๋กœ where์•„๋‹Œ having์„ ์“ด๋‹ค)
group by job 
having (avg(sal)>=3000);


-- ๋ถ€์„œ๋ฒˆํ˜ธ๋ณ„ ํ‰๊ท  ๊ธ‰์—ฌ๊ฐ€ 2000์ด์ƒ์ธ ๋ถ€์„œ๋ฒˆํ˜ธ์™€ ํ‰๊ท  ๊ธ‰์—ฌ๋Š”?  
SELECT deptno, avg(sal)
from emp 
group by deptno 
having (AVG(sal)>2000);


-- ์—…๋ฌด๋ณ„ 1๋…„ ์†Œ๋“ํ‰๊ท ์ด 30000 ์ด์ƒ์ธ ์—…๋ฌด์™€ ํ‰๊ท ์€? ( 1๋…„ ์†Œ๋“ => 12๊ฐœ์›” ๊ธ‰์—ฌ + ์ปค๋ฏธ์…˜ )
SELECT job, avg(sal * 12 + ifnull(comm,0)) as "์†Œ๋“ํ‰๊ท "
from emp 
group by job 
having avg(sal * 12 + ifnull(comm,0))>=30000;


-- salesman ์—…๋ฌดํ•˜๋Š” ์ง์›๋“ค์˜ ํ‰๊ท ๊ธ‰์—ฌ๋Š”? 

SELECT job, AVG(sal)
from emp 
where job = 'salesman'

์ง‘๊ณ„ํ•จ์ˆ˜ having! 

ํ…Œ์ด๋ธ”๊ฐ’ where!