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
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'
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 [๊ฒ์์กฐ๊ฑด]
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 [๊ฒ์์กฐ๊ฑด]
select *
from nasdaq_company as a
left outer join industry_group_symbol as b
on a.symbol = b.symbol
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 [๊ฒ์ ์กฐ๊ฑด]
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'
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'))
select [์ด ์ด๋ฆ]
from [ํ
์ด๋ธ]
where [์ด] in (select [์ด] from [ํ
์ด๋ธ])
select *
from nasdaq_company
where symbol in (select symbol from nasdaq_company where symbol in ('msft', 'a'))
์๋ธ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์์ ๊ฐ์ด ํ๋๋ผ๋ ๋ง์กฑํ๋ ์กฐ๊ฑด ๊ฒ์
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 : ์กฐ๊ฑด์ ๊ฒฐ๊ด๊ฐ์ด ์๋์ง ์๋์ง ํ์ธํ๊ณ 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')
)
์๋ธ ์ฟผ๋ฆฌ ๊ฒฐ๊ด๊ฐ์ ์๋ ๋ชจ๋ ๊ฐ์ ๋ง์กฑํ๋ ์กฐ๊ฑด์ ์ฃผ ์ฟผ๋ฆฌ์์ ๊ฒ์ํด ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ