๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ž๊ฒฉ์ฆ/SQLD

[์˜ค๋‹ต๋…ธํŠธ] 2-1. SQL ๊ธฐ๋ณธ

by kime2 2024. 3. 3.
๐ŸŽˆSQL ๋ฌธ์žฅ๋“ค์˜ ์ข…๋ฅ˜
1. DML: data manipulation language
- select = ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋“ค์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด, retrieve๋ผ๊ณ ๋„ ํ•จ
- insert, update, delete = ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์— ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋ณ€ํ˜•์„ ๊ฐ€ํ•˜๋Š” ์ข…๋ฅ˜์˜ ๋ช…๋ น์–ด๋“ค(์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ)
2. DDL : data definition language
- create, alter, drop, rename : ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋“ค๋กœ, ๊ทธ๋Ÿฌํ•œ ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ/๋ณ€๊ฒฝ/์‚ญ์ œ/์ด๋ฆ„๋ณ€๊ฒฝ ํ•˜๋Š”๋ฐ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด
-> Alter table ํ…Œ์ด๋ธ” modify(์ปฌ๋Ÿผ, ํƒ€์ž…)
3. DCL : data control language
- grant, revoke : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ณ  ๊ฐ์ฒด๋“ค์„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒํ•œ์„ ์ฃผ๊ณ  ํšŒ์ˆ˜ํ•˜๋Š” ๋ช…๋ น์–ด
4. TCL : transaction control language 
- commit, rollback : ๋…ผ๋ฆฌ์ ์ธ ์ž‘์—…์˜ ๋‹จ์œ„๋ฅผ ๋ฌถ์–ด์„œ DML์— ์˜ํ•ด ์กฐ์ž‘๋œ ๊ฒฐ๊ณผ๋ฅผ ์ž‘์—…๋‹จ์œ„(ํŠธ๋žœ์žญ์…˜) ๋ณ„๋กœ ์ œ์–ดํ•˜๋Š” ๋ช…๋ น์–ด

๐ŸŽˆSELECT ๋ฌธ
SELECT (all/distinct) ๋ณด๊ณ ์‹ถ์€ ์นผ๋Ÿผ๋ช…...
FROM ํ…Œ์ด๋ธ”
- all : ๊ธฐ๋ณธ์˜ต์…˜, ๋ชจ๋“ ์นผ๋Ÿผ๋“ค
- distinct : ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ 1๊ฑด์œผ๋กœ ์ฒ˜๋ฆฌ

๐ŸŽˆ NULL๊ณผ ์—ฐ์‚ฐ
๋„๊ฐ’๊ณผ์˜ ์‚ฌ์น™์—ฐ์‚ฐ์„ ๋„๊ฐ’๋ฆฌํ„ด(์ง‘๊ณ„ํ•จ์ˆ˜๋Š” ๋„์„ ์ œ์™ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ž‘๋™)
๋„๊ฐ’๊ณผ์˜ ๋น„๊ต์—ฐ์‚ฐ์€ false ๋ฆฌํ„ด
ํŠน์ •๊ฐ’๊ณผ์˜ ๋น„๊ต ๋ถˆ๊ฐ€
1. ํ‘œํ˜„์‹1์ด ๋„๊ฐ’์ด๋ฉด ํ‘œํ˜„์‹ 2๋กœ ๋Œ€์ฒด
[์˜ค] NVL(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
[์„œ] ISNULL(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
2. ํ‘œํ˜„์‹1์ด ํ‘œํ˜„์‹2์™€ ๊ฐ™์œผ๋ฉด ๋„๊ฐ’, ์•„๋‹ˆ๋จ„ ํ‘œํ˜„์‹1 ์œ ์ง€
NULLIF(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
3. ํ‘œํ˜„์‹1์ด ๋„๊ฐ’์ด๋ฉด ํ‘œํ˜„์‹2, ํ‘œํ˜„์‹2๊ฐ€ ๋„๊ฐ’์ด๋ฉด ํ‘œํ˜„์‹...
COALESCE(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2..)

๐ŸŽˆ EQUI JOIN ๋ฌธ์žฅ
์กฐ์ธ์— ๊ด€์—ฌํ•˜๋Š” ํ…Œ์ด๋ธ”๊ฐ„์˜ ์นผ๋Ÿผ๊ฐ’๋“ค์ด ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ

1. WHERE ์ ˆ์— JOIN์กฐ๊ฑด์„ ๋„ฃ๋Š”๋‹ค
SELECT ํ…Œ์ด๋ธ”1.์ปฌ๋Ÿผ, ํ…Œ์ด๋ธ”2.์ปฌ๋Ÿผ
FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2
WHERE ํ…Œ์ด๋ธ”1.์ปฌ๋Ÿผ1 = ํ…Œ์ด๋ธ”2.์ปฌ๋Ÿผ2;

2. ON ์ ˆ์— JOIN์กฐ๊ฑด์„ ๋„ฃ๋Š”๋‹ค
SELECT ํ…Œ์ด๋ธ”1.์ปฌ๋Ÿผ, ํ…Œ์ด๋ธ”2.์ปฌ๋Ÿผ
FROM ํ…Œ์ด๋ธ”1 JOIN ํ…Œ์ด๋ธ”2 
ON ํ…Œ์ด๋ธ”1.์ปฌ๋Ÿผ1 = ํ…Œ์ด๋ธ”2.์ปฌ๋Ÿผ2;

๐ŸŽˆ์ง‘ํ•ฉ์—ฐ์‚ฐ์ž
์กฐ๊ฑด: ์ง‘ํ•ฉ์—ฐ์‚ฐ์ด ๋˜๋Š” ๋‘ ์งˆ์˜๋Š” ์ปฌ๋Ÿผ์ˆ˜ ๋™์ผ, ์ƒํ˜ธํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ ํƒ€์ž…, ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ผญ ๋ณ„์นญ์„ ์‚ฌ์šฉ
1. UNION : ์ค‘๋ณต์ œ๊ฑฐ๊ฐ€ ๋œ ํ•ฉ์ง‘ํ•ฉ
2. UNION ALL : ์ค‘๋ณต์ด ํ—ˆ์šฉ๋œ ํ•ฉ์ง‘ํ•ฉ(์†๋„๋น ๋ฆ„) 
3. INTERSECT : ์ค‘๋ณต์ œ๊ฑฐ๋œ ๊ต์ง‘ํ•ฉ
4. MINUS(ORACLE)/ EXCEPT(MS_SQL): ์•ž์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์—์„œ ๋’ค์˜ SQL๊ฒฐ๊ณผ๋ฅผ ๋บ€ ์ฐจ์ง‘ํ•ฉ


๐ŸŽˆ์ •๋ ฌ
๊ธฐ๋ณธ์ ์ธ ์ •๋ ฌ์€ ์˜ค๋ฆ„์ฐจ์ˆœ
์˜ค๋ผํด์—์„œ ๋„๊ฐ’์„ ๊ฐ€์žฅ ํฐ ์ˆœ์œผ๋กœ ๊ฐ„์ฃผ, SQL server์€ ๊ฐ€์žฅ ์ž‘์€์ˆœ์œผ๋กœ ๊ฐ„์ฃผ
์ผ๋ฐ˜์ ์œผ๋กœ select์ ˆ์— ์—†๋Š” ์ปฌ๋Ÿผ ๋ฐ ๊ณ„์‚ฐ์œผ๋กœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ง‘๊ณ„ํ•จ์ˆ˜์‹œ์—๋Š” ๊ผญ ๋ช…์‹œ๋œ ์ปฌ๋Ÿผ๋งŒ ๊ฐ€๋Šฅ

๐ŸŽˆ์ˆœ์ˆ˜๊ด€๊ณ„์—ฐ์‚ฐ์ž
select์—ฐ์‚ฐ์€ where์ ˆ๋กœ ๊ตฌํ˜„
project ์—ฐ์‚ฐ์€ select์ ˆ๋กœ ๊ตฌํ˜„
join ์—ฐ์‚ฐ์€ ๋‹ค์–‘ํ•œ join๊ธฐ๋Šฅ์„ ๊ตฌํ˜„
divide ํ˜„์žฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ

 

1.

CREATE TABLE ์„œ๋น„์Šค
(
์„œ๋น„์Šค๋ฒˆํ˜ธ varchar2(10) primary key,
์„œ๋น„์Šค๋ช… varchar2(100) null,
๊ฐœ์‹œ์ผ์ž date not null );

#ใ„ฑ
select * from ์„œ๋น„์Šค where ์„œ๋น„์Šค๋ฒˆํ˜ธ = 1; 
#ใ„ด
insert into ์„œ๋น„์Šค value ('999', '' , '2015-11-11');
#ใ„ท
select * from ์„œ๋น„์Šค where ์„œ๋น„์Šค๋ช… = '';
#ใ„น
select * from ์„œ๋น„์Šค where ์„œ๋น„์Šค๋ช… is null

 

  • ์„œ๋น„์Šค๋ฒˆํ˜ธ ์นผ๋Ÿผ์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๊ฐ€ '001'๊ณผ ๊ฐ™์€ ์ˆซ์žํ˜•์‹์œผ๋กœ ์ž…๋ ฅ๋˜์–ด ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค
  • ใ„ด๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ( ' ' ) ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์„œ๋น„์Šค๋ช… ์นผ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์˜ค๋ผํด์—์„œ๋Š” NULL ๊ฐ’์œผ๋กœ ์ž…๋ ฅ๋œ๋‹ค
  • ใ„ด๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์–ด์žˆ์„ ๋•Œ ์˜ค๋ผํด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ ค๋ฉด ์„œ๋น„์Šค๋ช… is null ์กฐ๊ฑด์œผ๋กœ ์ž…๋ ฅํ•œ๋‹ค 
  • ใ„ด๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์–ด์žˆ์„ ๋•Œ SQL server ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ ค๋ฉด ์„œ๋น„์Šค๋ช… = ' ' ์กฐ๊ฑด์œผ๋กœ ์ž…๋ ฅํ•œ๋‹ค

2. to chart ์™€ to date

  • where to_chart(end_date, 'YYYYMM')=  '201501' -> 2015๋…„ 1์›” 31์ผ 23์‹œ 59๋ถ„
  • -> chart์ผ ๊ฒฝ์šฐ 'YYYYMM'์˜ 'day๋Š” ์›”๋ง
  • where to_date('201501', 'YYYYMM') ='end_date -> 2015๋…„ 1์›” 1์ผ 00์‹œ 00๋ถ„
  • -> date์ผ ๊ฒฝ์šฐ 'YYYYMM'์˜ 'day๋Š” ์›”์ดˆ

 

3.  2023๋…„ 1์›” 10์‹œ 10๋ถ„

select to_chart(to_date('2023.01.10.10', 'YYYY.MM.DD HH24')
# 2023๋…„ 01์›”10์ผ 10์‹œ์—์„œ
+ 1/24/(60/10),'YYYY.MM.DD HH24:Mi:SS') from dual
# 10๋ถ„์„ ๋”ํ•จ

1/24/60 = 1๋ถ„

1/24/(60/10) = 10๋ถ„

 

4. case ๋ฌธ

case when loc = 'new york' then 'east' else 'etc' end as area
case loc when 'new york' then 'east' else 'etc' end as area
decode(loc, 'new york', 'east', 'ect' )

 

5. ๊ด‘๊ณ ๋งค์ฑ„ ID๋ณ„ ์ตœ์ดˆ๋กœ ๊ฒŒ์‹œํ•œ ๊ด‘๊ณ ๋ช…๊ณผ ๊ด‘๊ณ ์‹œ์ž‘์ผ ์ถœ๋ ฅ

[๊ด‘๊ณ ]๊ด‘๊ณ ID [๊ด‘๊ณ ๊ฒŒ์‹œ] ๊ด‘๊ณ ๊ฒŒ์‹œ๋ฒˆํ˜ธ [๊ด‘๊ณ ๋งค์ฒด] ๊ด‘๊ณ ๋งค์ฒดID
๊ด‘๊ณ ๋ช… ๊ด‘๊ณ ID(FK)
๊ด‘๊ณ ๋งค์ฒดID(FK)
๊ด‘๊ณ ์‹œ์ž‘์ผ์ž
๊ด‘๊ณ ์ข…๋ฃŒ์ผ์ž
๊ด‘๊ณ ๋งค์ฒด๋ช…

 

from ๊ด‘๊ณ ๊ฒŒ์‹œ a, ๊ด‘๊ณ  b, ๊ด‘๊ณ ๋งค์ฒด c,
	(select ๊ด‘๊ณ ๋งค์ฒดID, MIN(๊ด‘๊ณ ์‹œ์ž‘์ผ)as ๊ด‘๊ณ ์‹œ์ž‘์ผ์ž
    from ๊ด‘๊ณ ๊ฒŒ์‹œ
    group by ๊ด‘๊ณ ๋งค์ฒดid) d
where a.๊ด‘๊ณ ์‹œ์ž‘์ผ์ž = d.๊ด‘๊ณ ์‹œ์ž‘์ผ์ž 
and a.๊ด‘๊ณ ๋งค์ฒดid = d.๊ด‘๊ณ ๋งค์ฒดid
and a.๊ด‘๊ณ id = b.๊ด‘๊ณ id
and a.๊ด‘๊ณ ๋งค์ฒดid = c.๊ด‘๊ณ ๋งค์ฒดid
order by c.๊ด‘๊ณ ๋งค์ฒด๋ช…

 

6. [ORACLE] order by + case when

ID
100
100
200
200
200
999
999

 

select ID from TBL
group by ID
having count(*) = 2
order by (case when ID = 999 then 0 else ID end)
# ID๋ณ„๋กœ ๊ทธ๋ฃนํ™”๋ฅผ ํ•ด์„œ ๊ฐฏ์ˆ˜๊ฐ€ 2์ธ ๊ฒƒ๋งŒ ์ถœ๋ ฅ์„ ํ•˜๋Š”๋ฐ, ์ •๋ ฌ์„ ID๊ฐ€ 999์ธ ๊ฒƒ์„ 0๋ฒˆ์งธ๋กœ ์ •๋ ฌํ•œ๋‹ค
ID
100
999

 

7. to_date ์™€ to_chart

select to_chart(to_date('2019.02.05','YYYY.MM.DD') + 1/12/(60/30), 'YYYY.MM.DD HH24:MI:SS')
from dual;

1) to_date('2019.02.05','YYYY.MM.DD') = 2019.02.05 -> ๋‚ ์งœ๋กœ ๋ฐ”๊พธ๊ธฐ 

2) 2019.02.05 + 1/12/(60/30) = 2019๋…„ 2์›” 5์ผ 1์‹œ

  • 1/24 = 1์‹œ๊ฐ„ = 1์ผ(24์‹œ๊ฐ„)์„ 24๋กœ ๋‚˜๋ˆ„๊ธฐ(60๋ถ„)
  • 1/24/60 = 1๋ถ„ = 1์ผ์„ 24์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„๊ณ  60๋ถ„์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
  • 1/24/(60/10) = 10๋ถ„ = 1์ผ์„ 24์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„๊ณ  6๋ถ„์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
  • 1/12/(60/30) = 1์‹œ๊ฐ„ = 1์ผ์„ 12์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„๊ณ (2์‹œ๊ฐ„) 2๋ถ„์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
  • 1.5/24 = 1์‹œ๊ฐ„ 20๋ถ„
  • 1.5/24/6 = 15๋ถ„

8. JOIN์— ๋Œ€ํ•œ ์„ค๋ช…

  • ์ผ๋ฐ˜์ ์œผ๋กœ ์กฐ์ธ์€ PK์™€ FK์˜ ์—ฐ๊ด€์„ฑ์— ์˜ํ•ด ์„ฑ๋ฆฝ๋œ๋‹ค
  • DBMS์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” FROM์ ˆ์— ๋‚˜์—ด๋œ ํ…Œ์ด๋ธ”์„ ์ž„์˜๋กœ 2๊ฐœ์ •๋„์”ฉ ๋ฌถ์–ด์„œ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค
  • EQUI JOIN์€ ์กฐ์ธ์— ๊ด€์—ฌํ•˜๋Š” ํ…Œ์ด๋ธ” ๊ฐ„์˜ ์นผ๋Ÿผ๋“ค์ด ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค
  • EQUI JOIN์€ '=' ์—ฐ์‚ฐ์ž์— ์˜ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰๋˜๋ฉฐ, ๊ทธ ์ด์™ธ์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋ชจ๋‘ NON EQUI JOIN์ด๋‹ค
  • ๋Œ€๋ถ€๋ถ„ NON EQUI ์กฐ์ธ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋•Œ๋กœ๋Š” ์„ค๊ณ„์ƒ์˜ ์ด์œ ๋กœ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค

 

9. 

[์ œํ’ˆ]
์ œํ’ˆ์ฝ”๋“œ
[์ƒ์‚ฐ์ œํ’ˆ]
๋ผ์ธ๋ฒˆํ˜ธ(FK)
์ œํ’ˆ์ฝ”๋“œ(FK)
[์ƒ์‚ฐ๋ผ์ธ]
๋ผ์ธ๋ฒˆํ˜ธ
์ œํ’ˆ๋ช…
์ œํ’ˆ์œ ํ˜•์ฝ”๋“œ
๋‹จ์œ„
  ์ตœ์ข…๊ฐ€๋™์ผ์ง€

 

  • ์ œํ’ˆ, ์ƒ์‚ฐ๋ผ์ธ, ์ƒ์‚ฐ์ œํ’ˆ ์—”ํ„ฐํ‹ฐ๋ฅผ inner joinํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์‚ฐ์ œํ’ˆ ์—”ํ„ฐํ‹ฐ๋Š” where์ ˆ์— ์ตœ์†Œ 2๋ฒˆ ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค
  • ์ œํ’ˆ๊ณผ ์ƒ์‚ฐ๋ผ์ธ ์—”ํ„ฐํ‹ฐ๋ฅผ join์‹œ ์ ์ ˆํ•œ join์กฐ๊ฑด์ด ์—†์œผ๋ฏ€๋กœ ์นดํ‹ฐ์‹œ์•ˆ ๊ณฑ์ด ๋ฐœ์ƒํ•œ๋‹ค
  • * ์นดํ‹ฐ์‹œ์•ˆ๊ณฑ = FROM์ ˆ์— 2๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์ด ์žˆ์„ ๋•Œ ๋‘ ํ…Œ์ด๋ธ” ์‚ฌ์ด์— ์œ ํšจํ•œ join์กฐ๊ฑด์„ ์ ์ง€ ์•Š์•˜์„๋•Œ ํ•ด๋‹นํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ๊ฒฐํ•ฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์˜ ํ–‰ ๊ฐฏ์ˆ˜๋ฅผ ๊ณฑํ•œ ๋งŒํผ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค -> ์กฐ์ธ์กฐ๊ฑด์„ ์ƒ๋žต/์กฐ์ธ์กฐ๊ฑด์ด ์ž˜๋ชป๋จ ๋“ฑ
  • * cross ์กฐ์ธ = ์นดํ‹ฐ์‹œ์•ˆ๊ณฑ์„ ํ‘œ์ค€ํ™”ํ•˜์—ฌ ๋งŒ๋“  ์กฐ์ธ
  • ํŠน์ • ์ƒ์‚ฐ๋ผ์ธ๋ฒˆํ˜ธ์—์„œ ์ƒ์‚ฐ๋˜๋Š” ์ œํ’ˆ์˜ ์ œํ’ˆ๋ช…์„ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ œํ’ˆ, ์ƒ์‚ฐ๋ผ์ธ๊นŒ์ง€ 2๊ฐœ์˜ ์—”ํ„ฐํ‹ฐ๋งŒ ์กฐ์ธํ•˜๋ฉด ๋œ๋‹ค

 

10.cross ์กฐ์ธ

ํ…Œ์ด๋ธ”๊ฐ„ ์กฐ์ธ ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ์กฐํ•ฉ(M*N)

#[1]
select ename, dname
from emp, dept

#[2]
select ename, dname
from emp cross join dept

 

11. ๋™์ผํ•œ join ๊ฒฐ๊ณผ

# using ์ ˆ -> using์ ˆ์—๋Š” ๋ณ„์นญ X
select t.region-name, t.team_name, t.stadium_id, s.stadium_name
from team t inner join stadium s
using(stadium_id)

# on์ ˆ -> ๋ณ„์นญ์‚ฌ์šฉ๊ฐ€๋Šฅ
select team.region-name, team.team_name, team.stadium_id, stadium.stadium_name
from team inner join stadium
on team.stadium_id = stadium.stadium_id

# where ์ ˆ -> ๋ณ„์นญ
select t.region-name, t.team_name, t.stadium_id, s.stadium_name
from team t , stadium s
where t.stadium_id = s.stadium_id

# where ์ ˆ
select team.region-name, team.team_name, team.stadium_id, stadium.stadium_name
from team , stadium
where team.stadium_id = stadium.stadium_id

#natural join -> ๋‘ํ…Œ์ด๋ธ”๊ฐ„ ๋™์ผํ•œ ์นผ๋Ÿผ์ด stadium_id๋ฐ–์— ์—†๋Š” ๊ฒฝ์šฐ
select region-name, team_name, stadium_id, stadium_name
from team natural join stadium

*USING = on์ ˆ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ where์ ˆ ๋Œ€์‹  join์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ์“ธ๋•Œ ์‚ฌ์šฉ, join์„ ๋งŒ์กฑํ•˜๋Š” ์ปฌ๋Ÿผ๋งŒ ๊ด„ํ˜ธ์— ์ž…๋ ฅ

-- where ์ ˆ
select a.name, b,age
from student a, data b
where a.id = b.id;

-- on ์ ˆ
select a.name, b,age
from student a join data b
on a.id = b.id;

-- using ์ ˆ
select a.name, b,age
from student a join data b
using (id);

 

11. outer join๊ณผ union

#[1]
select a.id, b.id
from tbl a full outer join tbl2 b
on a.id = b.id

#[2]
select a.id, b.id
from tbl a left outer join tbl2 b
on a.id = b.id
union
select a.id, b.id
from tbl a right outer join tbl2 b
on a.id = b.id

#[3]
select a.id, b.id
from tbl a , tbl2 b
where a.id = b.id # inner
union all
select a.id, null
from tbl a 
where not exists(select 1 from tbl2 b where a.id = b.id) #a-b์ฐจ์ง‘ํ•ฉ
union all
select b.id, null
from tbl b 
where not exists(select 1 from tbl2 a where a.id = b.id) #b-a์ฐจ์ง‘ํ•ฉ

 

0. SQL ์˜ค๋ฅ˜

select ์ง€์—ญ, sum(๋งค์ถœ๊ธˆ์•ก)
from ์ง€์—ญ๋ณ„๋งค์ถœ
group by ์ง€์—ญ
order by ๋…„ desc #์ง€์—ญ๋ณ„๋กœ ๊ทธ๋ฃนํ™”๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— select ์ ˆ์— ๊ธฐ์ˆ ๋˜์ง€ ์•Š๋Š” '๋…„'์œผ๋กœ ์ •๋ ฌ๋  ์ˆ˜ ์—†๋‹ค