
ํ๋์ SQL ๋ฌธ ์์ ํฌํจ๋์ด ์๋ ๋ ๋ค๋ฅธ SQL ๋ฌธ
(๋ฉ์ธ์ฟผ๋ฆฌ๊ฐ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ํฌํจํ๋ ์ข ์์ ์ธ ๊ด๊ณ)โข ์๋ธ์ฟผ๋ฆฌ๋ ๋ฉ์ธ์ฟผ๋ฆฌ์ ์นผ๋ผ ์ฌ์ฉ ๊ฐ๋ฅ โข ๋ฉ์ธ์ฟผ๋ฆฌ๋ ์๋ธ์ฟผ๋ฆฌ์ ์นผ๋ผ ์ฌ์ฉ ๋ถ๊ฐ๐ซ ์ฃผ์์ฌํญ
โข ์๋ธ์ฟผ๋ฆฌ๋ ๊ดํธ๋ก ๋ฌถ์ด์ ์ฌ์ฉ โข ๋จ์ผ ํ ํน์ ๋ณต์ ํ ๋น๊ต ์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ ๊ฐ๋ฅ โข ์๋ธ์ฟผ๋ฆฌ ์์๋ order by ์ฌ์ฉ๋ถ๊ฐ
์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ (Scalar Subquery) - SELECT ์ ์ ์ฌ์ฉ
์ธ๋ผ์ธ ๋ทฐ (Inline View) - FROM ์ ์ ์ฌ์ฉ
์ค์ฒฉ ์๋ธ์ฟผ๋ฆฌ (Nested Subquery) - WHERE ์ ์ ์ฌ์ฉ๐ Nested Subquery
Single Row - ํ๋์ ์ด์ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
Multiple Row - ํ๋ ์ด์์ ์ด์ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
Multiple Column - ํ๋ ์ด์์ ํ์ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
SCALAR SUBQUERY ๋ฌธ๋ฒ
select column1, (select column2 from table2 where condition)
from table1
where condition
๐ซ ๊ฒฐ๊ณผ๋ ํ๋์ Column ์ด์ด์ผ ํจ
INLINE VIEW ๋ฌธ๋ฒ
select a.column, b.column
from table1 a, (select column1, column2 from table2) b
where condition
๐ซ ๋ฉ์ธ์ฟผ๋ฆฌ์์๋ ์ธ๋ผ์ธ ๋ทฐ์์ ์กฐํํ Column ๋ง ์ฌ์ฉ๊ฐ๋ฅ
NESTED SUBQUERY ๋ฌธ๋ฒ
- single row
//์๋ธ์ฟผ๋ฆฌ๊ฐ ๋น๊ต์ฐ์ฌ์์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ
select column_name from tablename
where column_name = (select column_name from tablename where condition)
order by column_name
๐ซ ์๋ธ์ฟผ๋ฆฌ์ ๊ฒ์ ๊ฒฐ๊ณผ๋ ํ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๊ฐ์ ธ์ผํจ (๋๊ฐ ์ด์์ธ ๊ฒฝ์ฐ ์๋ฌ)
๐ซ ๊ดํธ ์์ผ๋ฉด ์๋ฌ ๋ฐ์
-Multiple Row - IN
//์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค์ ํฌํจ๋๋ ๊ฒฝ์ฐ
select column_name from tablename
where column_name in (select column_name from tablename where condition)
order by column_name
`
-Multiple Row - EXISTS
//์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์ ๊ฐ์ด ์์ผ๋ฉด ๋ฐํ
select column_name from tablename
where exists (select column_name from tablename where condition)
order by column_name
-Multiple Row - ANY
//์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค์ ์ต์ํ ํ๋๋ผ๋ ๋ง์กฑํ๋ ๊ฒฝ์ฐ (๋น๊ต์ฐ์ฐ์ ์ฌ์ฉ)
select column_name from tablename
where column_name = any (select column_name from tablename where condition)
order by column_name
-Multiple Row - ALL
//์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ ๋ง์กฑํ๋ ๊ฒฝ์ฐ(๋น๊ต ์ฐ์ฐ์ ์ฌ์ฉ)
select column_name from tablename
where column_name = all (select column_name from tablename where condition)
order by column_name
-Multi Column Subquery - ์ฐ๊ด ์๋ธ์ฟผ๋ฆฌ
//์๋ธ์ฟผ๋ฆฌ ๋ด์ ๋ฉ์ธ์ฟผ๋ฆฌ ์ปฌ๋ผ์ด ๊ฐ์ด ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ
select column_name from tablename a
where (a.column1, a.column2, ...) (select b.column1, b.column2, ... from tablename b where a.column = b.column)
order by column_name
- ๊ณต๊ณต๋ฐ์ดํฐ csv ํ์ผ๋ก ์ฐ์ตํ๊ธฐ
5๋๋ฒ์ฃ ๋ฐ์ ๊ฒ๊ฑฐ ํํฉ
5๋๋ฒ์ฃ ๋ฐ์ ์ฅ์๋ณ ํํฉ
![]() | ![]() |
|---|
select ANY_VALUE(์ฅ์) ์ฅ์, max(๋ฐ์๊ฑด์) ๋ฐ์๊ฑด์,
(select sum(๊ฑด์) from ์ง์ญ๋ณ_5๋๋ฒ์ฃ where ์ฃ์ข
='์ด์ธ' and ๋ฐ์๊ฒ๊ฑฐ='๋ฐ์') ์ด๋ฐ์๊ฑด์
from ์ฅ์๋ณ_5๋๋ฒ์ฃ
where ๋ฒ์ฃ๋ช
='์ด์ธ'
โ๏ธsql_mode=only_full_group_by ์๋ฌ
group by ์ ์ ํฌํจ๋์ง ์๋ column ์ select ํ ๊ฒฝ์ฐ ๋ฐ์ํ๋ ์๋ฌ
ํด๊ฒฐ๋ฐฉ์
ANY_VALUE(column)ํจ์ ์ฌ์ฉ
select ์ง์ญ, ์ฃ์ข
์ ํ, ๊ฑด์ ๋ฐ์๊ฑด์
from ์ง์ญ๋ณ_5๋๋ฒ์ฃ a,
(select ์ง์ญ ์ง์ญ๊ตฌ๋ถ, max(๊ฑด์) ๋ฐ์๊ฑด์ from ์ง์ญ๋ณ_5๋๋ฒ์ฃ where ๋ฐ์๊ฒ๊ฑฐ='๋ฐ์' group by ์ง์ญ๊ตฌ๋ถ) b
where a.์ง์ญ = b.์ง์ญ๊ตฌ๋ถ and a.๊ฑด์=b.๋ฐ์๊ฑด์
order by ๊ฑด์ desc
limit 5;
select ์ง์ญ, ์ฃ์ข
๋ฒ์ฃ์ ํ
from ์ง์ญ๋ณ_5๋๋ฒ์ฃ
where ๊ฑด์ in (select max(๊ฑด์) from ์ง์ญ๋ณ_5๋๋ฒ์ฃ where ๋ฐ์๊ฒ๊ฑฐ='๋ฐ์');
select distinct(์ง์ญ),์ฃ์ข
๋ฒ์ฃ์ ํ
from ์ง์ญ๋ณ_5๋๋ฒ์ฃ a
where exists
(select ์ง์ญ, ์ฃ์ข
from ์ง์ญ๋ณ_5๋๋ฒ์ฃ b where a.์ง์ญ = b.์ง์ญ and ๋ฐ์๊ฒ๊ฑฐ='๋ฐ์' and a.์ฃ์ข
=b.์ฃ์ข
and ๊ฑด์ >=2000);
์ค์ตํ์ธ
![]() ![]() | ![]() |
|---|
![]() |
|---|