SQL - join

parkeuยท2022๋…„ 10์›” 18์ผ
1

ABC๋ถ€ํŠธ์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
43/55

๋‚ด๋ถ€์กฐ์ธ

๊ธฐ๋ณธํ˜•์‹

select [์—ด์ด๋ฆ„]
from [ํ…Œ์ด๋ธ”1]
inner join [ํ…Œ์ด๋ธ”2] on [ํ…Œ์ด๋ธ”1.์—ด] = [ํ…Œ์ด๋ธ”2.์—ด]
where [๊ฒ€์ƒ‰์กฐ๊ฑด]
select *
from nasdaq_company as a
inner join stock as b
on a.symbol = b.symbol

์ปฌ๋Ÿผ๋ช…์„ ๋‹ค ์ ์–ด์ค€ ์ฝ”๋“œ + where

select a.symbol, 
a.company_name, 
a.sector, 
b.date, 
b.high, 
b.volume
from nasdaq_company as a
inner join stock as b
on a.symbol = b.symbol
where a.symbol ='msft' and b.date >= '2021-10-01' and b.date < '2021-11-01'

๋‘๊ฐœ ์ด์ƒ์˜ ์กฐ๊ฑด ์ ์šฉํ•œ inner join ๋ฌธ

select a.symbol, a.last_crawel_date, b.date
from nasdaq_company as a
inner join stock as b on a.symbol = b.symbol and a.last_crawel_date = b.date
where a.symbol = 'msft'

์„ธ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” ์กฐ์ธ

๊ธฐ๋ณธ ํ˜•์‹

select [์—ด์ด๋ฆ„]
from [ํ…Œ์ด๋ธ”1]
inner join [ํ…Œ์ด๋ธ”2] on [ํ…Œ์ด๋ธ”1.์—ด] = [ํ…Œ์ด๋ธ”2.์—ด]
inner join [ํ…Œ์ด๋ธ”3] on [ํ…Œ์ด๋ธ”2.์—ด] = [ํ…Œ์ด๋ธ”3.์—ด]
where [๊ฒ€์ƒ‰์กฐ๊ฑด]

์„ธ๊ฐœ ์ด์ƒ ํ…Œ์ด๋ธ”์„ inner join์œผ๋กœ ๊ฒ€์ƒ‰

select a.industry,
       c.symbol, 
	   c.company_name, 
	   c.ipo_year, 
	   c.sector
from industry_group as a
    inner join industry_group_symbol as b 
	    on a.num = b.num
	inner join nasdaq_company as c 
	    on b.symbol = c.symbol
where a.industry = N'์ž๋™์ฐจ'
order by symbol


์™ธ๋ถ€์กฐ์ธ

๊ธฐ๋ณธํ˜•์‹

select [์—ด์ด๋ฆ„]
from [ํ…Œ์ด๋ธ”1]
<left, right, full> outer join [ํ…Œ์ด๋ธ”2] on [ํ…Œ์ด๋ธ” 1.์—ด]=[ํ…Œ์ด๋ธ” 2.์—ด]
where [๊ฒ€์ƒ‰์กฐ๊ฑด]

2๊ฐœ ํ…Œ์ด๋ธ”์„ left outer join ์ฟผ๋ฆฌ์‹คํ–‰

select *
from nasdaq_company as a
     left outer join industry_group_symbol as b
	 on a.symbol = b.symbol

2๊ฐœ ํ…Œ์ด๋ธ”์„ right outer join ์ฟผ๋ฆฌ ์‹คํ–‰

select *
from nasdaq_company as a
     right outer join industry_group_symbol as b
	 on a.symbol = b.symbol


๊ต์ฐจ์กฐ์ธ

์ž์ฃผ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์Œ

๊ธฐ๋ณธ ํ˜•์‹

select [์—ด์ด๋ฆ„]
from [ํ…Œ์ด๋ธ” 1]
cross join [ํ…Œ์ด๋ธ” 2]
where [๊ฒ€์ƒ‰ ์กฐ๊ฑด]

cross join ์ฟผ๋ฆฌ

create table doit_cross1(num int)
create table doit_cross2(name nvarchar(10))
insert into doit_cross1 values(1), (2), (3)
insert into doit_cross2 values('Do'),('It'),('SQL')

select *
from doit_cross1
     cross join doit_cross2

์…€ํ”„ ์กฐ์ธ

๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ํ•œ ์กฐ์ธ
๋ฐ˜๋“œ์‹œ ๋ณ„์นญ ์‚ฌ์šฉํ•ด์•ผ !!

select a.symbol, 
	   a.date, 
	   a.[close], 
	   b.date, 
	   b.[close] - a.[close] as diff_close
from stock as a
    left outer join stock as b 
	    on a.date = dateadd(day, -1, b.date)
where a.symbol = 'msft' and b.symbol = 'msft'
and a.date >= '2021-10-01' and a.date < '2021-11-01'
and b.date >= '2021-10-01' and b.date < '2021-11-01'


p161 ํ€ด์ฆˆ
1. industry_group ํ…Œ์ด๋ธ”์—์„œ industry ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ Oil์— ํ•ด๋‹นํ•˜๋Š” symbol์„ industry_group_symbol ํ…Œ์ด๋ธ”์—์„œ ๊ฒ€์ƒ‰ํ•œ ๋‹ค์Œ, nasdaq_company ํ…Œ์ด๋ธ”์—์„œ ํ•ด๋‹น symbol์˜ company_name์„ ๊ฒ€์ƒ‰

select a.industry, b.symbol, c.company_name
from industry_group as a
	inner join industry_group_symbol as b
		on a.num = b.num
	inner join nasdaq_company as c
		on b.symbol = c.symbol
where a.industry = N'Oil'

  1. nasdaq_company ํ…Œ์ด๋ธ”์—์„œ industry_group_symbol ํ…Œ์ด๋ธ”์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” symbol, industry, company_name ๋ชฉ๋ก์„ ๊ฒ€์ƒ‰
select a.symbol, a.company_name
from nasdaq_company as a
	left outer join industry_group_symbol as b
		on a.symbol = b.symbol
where b.symbol is null


์„œ๋ธŒ์ฟผ๋ฆฌ

  • ๋ฐ˜๋“œ์‹œ ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ ์‚ฌ์šฉ
  • ์ฃผ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰
  • ๋น„๊ต ์—ฐ์‚ฐ์ž์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ์— ๊ธฐ์ˆ 
  • order by ๋ฌธ ์‚ฌ์šฉ ๋ถˆ๊ฐ€

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ

์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ 1ํ–‰๋งŒ ๋ฐ˜ํ™˜

๊ธฐ๋ณธ ํ˜•์‹

select [์—ด ์ด๋ฆ„]
from [ํ…Œ์ด๋ธ”]
where [์—ด] = (select [์—ด] from [ํ…Œ์ด๋ธ”])

๋‹ค์ค‘ ํ–‰ ์—ฐ์‚ฐ์ž ์ข…๋ฅ˜
IN : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์ž„์˜์˜ ๊ฐ’๊ณผ ๊ฐ™์€ ์กฐ๊ฑด ๊ฒ€์ƒ‰
ANY : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์–ด๋Š ํ•˜๋‚˜์˜ ๊ฐ’์ด๋ผ๋„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด ๊ฒ€์ƒ‰
EXISTS : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ
ALL : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด ๊ฒ€์ƒ‰

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ : ๊ฒ€์ƒ‰ ์„ฑ๊ณต

select *
from nasdaq_company
where symbol = (select symbol from nasdaq_company where symbol in('msft'))

๋‹ค์ค‘ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

IN ๋ฌธ, NOT IN ๋ฌธ

select [์—ด ์ด๋ฆ„]
from [ํ…Œ์ด๋ธ”]
where [์—ด] in (select [์—ด] from [ํ…Œ์ด๋ธ”])
select *
from nasdaq_company
where symbol in (select symbol from nasdaq_company where symbol in ('msft', 'a'))

ANY ๋ฌธ

์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์—์„œ ๊ฐ’์ด ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด ๊ฒ€์ƒ‰

select *
from nasdaq_company
where symbol = any (select symbol from nasdaq_company where symbol in ('msft','amd','amzn'))

< any ํ˜•ํƒœ : ์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด ์ตœ์†Ÿ๊ฐ’ ๋ฐ˜ํ™˜
> any ํ˜•ํƒœ : ์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด ์ตœ๋Œ“๊ฐ’ ๋ฐ˜ํ™˜

select *
from nasdaq_company
where symbol < any (select symbol from nasdaq_company where symbol in ('msft','ltch','zy'))

EXIST ๋ฌธ, NOT EXIST ๋ฌธ

EXIST : ์กฐ๊ฑด์˜ ๊ฒฐ๊ด๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๊ณ  1ํ–‰์ด๋ผ๋„ ์žˆ์œผ๋ฉด TRUE, ์—†์œผ๋ฉด FALSE ๋ฐ˜ํ™˜

SELECT * FROM nasdaq_company
WHERE EXISTS(
	SELECT symbol FROM nasdaq_company
	WHERE symbol IN ('MSFT','AMD','AMZN')
)


NOT EXIST

SELECT * FROM nasdaq_company
WHERE NOT EXISTS(
	SELECT symbol FROM nasdaq_company
	WHERE symbol IN ('MSFT','AMD','AMZN')
)

ALL๋ฌธ

์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ด๊ฐ’์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด์„ ์ฃผ ์ฟผ๋ฆฌ์—์„œ ๊ฒ€์ƒ‰ํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

profile
๋ฐฐ๊ณ ํŒŒ์šฉ.

0๊ฐœ์˜ ๋Œ“๊ธ€