๐ซต๐ป ๋ฐ์ดํฐ ๋ฒ ์ด์ค : ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋์ด ์๋ ํฐ ํด๋
SQL : SQL์ '์ฝ๋'๊ฐ ์๋๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์์ฒญ์ ํ๊ธฐ ์ํ '์ธ์ด'
SQL ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์์ฒญํ๋ ์ง์๋ฅผ QUERY๋ผ๊ณ ํจ
๐ธ ์๋ฌธ, ์ธ๋๋ฐ๋ ๋ณ๋ช ๋ง ์ ์ด๋ ๋์ง๋ง, ํน์๋ฌธ์ ๋ฐ ํ๊ธ์ ํฐ ๋ฐ์ดํ ์์ ๋ณ๋ช ๊ธฐ์ฌํด์ผํจ ๐ธ
๐ IF ) ์กฐ๊ฑด์ด ์ฌ๋ฌ ๊ฐ? -> ๋ ผ๋ฆฌ์ฐ์ฐ(and, or, not)์ผ๋ก ์ฐ๊ฒฐ์ํค๊ธฐ!
๐ IF ) ๋ฒ์ฃผ ๋ณ๋ก ๊ณ์ฐํ๊ณ ์ถ๋ค๋ฉด? -> GROUP BY ์ด์ฉ!
COUNT๋ ์ ์๋ฏธํ ๊ณ์ฐ์ผ๋ก ์ด์ฉํ ๋ ค๋ฉด GROUP BY ํ์์ ์ผ๋ก ํ์ํจ!
select
from
where
group by
having
order by
โ๐ป HAVING์ ๊ทธ๋ฃน ๋ณ๋ก ์ฐ์ฐํ ๋ค์ ์กฐ๊ฑด์ ์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉ
๐๐ป cast ํจ์ ์ด์ฉ
// ์ซ์๋ก ๋ณ๊ฒฝ
cast(if(rating = 'Not given', '1', rating) as decimal)
// ๋ฌธ์๋ก ๋ณ๊ฒฝ
concat(restaurant_name, '-', cast(order_id as char))
๐คญ QUERY ์์ QUERY
์ฌ๋ฌ ๋ฒ์ ์ฐ์ฐ์ ์ํํด์ผํ ๋
์กฐ๊ฑด๋ฌธ์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํด์ผ ํ ๋
์กฐ๊ฑด์ QUERY ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๋
์ฌ์ฉํ๋ฉด ์ ์ฉํจ
์๋ ์์ : ์์ชฝ ์ฟผ๋ฆฌ -> ๋ฐ๊นฅ์ชฝ ์ฟผ๋ฆฌ
SUBQUERY ์ข
๋ฅ
- ์ค์ฒฉ(์ผ๋ฐ) ์๋ธ์ฟผ๋ฆฌ - WHERE ์ ์์ ์ฌ์ฉ
- ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ - SELECT ์ ์์ ์ฌ์ฉ, ํ๋์ ์ปฌ๋ผ์ฒ๋ผ ์ฌ์ฉ
- ์ธ๋ผ์ธ ๋ทฐ(๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ) - FROM ์ ์์ ์ฌ์ฉ, ํ๋์ ํ
์ด๋ธ์ฒ๋ผ ์ฌ์ฉ, ๋ช
์นญ์ ๋ฐ๋์ ๊ธฐ์ฌ
UNION ํจ์ : ํ
์ด๋ธ ์์ง๊ฒฐํฉ
<๊ธฐ๋ณธ ๊ตฌ์กฐ>
# union/union all ๊ธฐ๋ณธ๊ตฌ์กฐ
# ์ปฌ๋ผ ์์๊ฐ ๊ฐ๊ณ , ๊ทธ ํ์์ด ๊ฐ์์ผ ํจ
select ์ปฌ๋ผ1, ์ปฌ๋ผ2, ์ปฌ๋ผ3..
from ํ
์ด๋ธ๋ช
1
union (all) #์์ง๊ฒฐํฉ ๋ช
์
select ์ปฌ๋ผ1, ์ปฌ๋ผ2, ์ปฌ๋ผ3..
from ํ
์ด๋ธ๋ช
2
JOIN : ํ
์ด๋ธ ์ํ๊ฒฐํฉ
SELECT *
FROM ํ
์ด๋ธ1 a LEFT JOIN ํ
์ด๋ธ2 b ON a.๊ณตํต ์ปฌ๋ผ = b.๊ณตํต ์ปฌ๋ผ
-> ๊ณตํต ์ปฌ๋ผ์ ๊ธฐ์ค์ผ๋ก ํ๋์ ํ
์ด๋ธ์ ๊ฐ์ด ์๋๋ผ๋ ๋ชจ๋ ์กฐํ
SELECT *
FROM ํ
์ด๋ธ1 a INNER JOIN ํ
์ด๋ธ2 b ON a.๊ณตํต ์ปฌ๋ผ = b.๊ณตํต ์ปฌ๋ผ
-> ๊ณตํต ์ปฌ๋ผ์ ๊ธฐ์ค์ผ๋ก ๋ ํ
์ด๋ธ ๋ชจ๋์ ์๋ ๊ฐ๋ง ์กฐํ
๐ซก ๊ณตํต ์ปฌ๋ผ์ '๊ณตํต ๊ฐ'์ด๊ธฐ ๋๋ฌธ์ ๋ ํ ์ด๋ธ์ ์ปฌ๋ผ๋ช ์ ๋ฌ๋ผ๋ ๊ด์ฐฎ์!
๊ณตํต์ปฌ๋ผ์ด ์๋ค๋ฉด, ์กฐ์ธ์ ํ ์ ์๋ค๋ ์ ์ ๊ธฐ์ต!
๊ณตํต์ปฌ๋ผ์ ๊ผญ ํ๋๊ฐ ์๋ ์ ์์ -> ์ฌ๋ฌ ๊ฐ๋ ๊ฐ๋ฅ
์๋ ERD, MAPPING CARDINALITY๋ก ํ์ธ
like 2์ธ, 150์ธ
-> ์กฐ๊ฑด๋ฌธ์ผ๋ก ์์์ ์ธ ์์ค์์์ ๋ฒ์๋ก ์ง์ ํด๋ฒ๋ฆฌ๊ธฐ
select restaurant_name,
max(if(hh='15', cnt_order, 0)) "15",
max(if(hh='16', cnt_order, 0)) "16",
max(if(hh='17', cnt_order, 0)) "17",
max(if(hh='18', cnt_order, 0)) "18",
max(if(hh='19', cnt_order, 0)) "19",
max(if(hh='20', cnt_order, 0)) "20"
from
(
select a.restaurant_name,
substring(b.time, 1, 2) hh,
count(1) cnt_order
from food_orders a inner join payments b on a.order_id=b.order_id
where substring(b.time, 1, 2) between 15 and 20
group by 1, 2
) a
group by 1
order by 7 desc
๐ซฅ window_function๊ณผ over์ ์ง๊ฟ!
<RANK ํจ์ ์์>
์์ ํ์
๋ณ๋ก ์ฃผ๋ฌธ ๊ฑด์๊ฐ ๊ฐ์ฅ ๋ง์ ์์ 3๊ฐ์ฉ ์กฐํํ๊ธฐ
select cuisine_type,
restaurant_name,
order_count,
rn "์์"
from
(
select cuisine_type,
restaurant_name,
rank() over (partition by cuisine_type order by order_count desc) rn,
order_count
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a
) b
where rn<=3
order by 1, 4
<SUM ํจ์ ์์>
๊ฐ ์์์ ์ ์ฃผ๋ฌธ๊ฑด์ด ํด๋น ์์ ํ์
์์ ์ฐจ์งํ๋ ๋น์จ์ ๊ตฌํ๊ณ , ์ฃผ๋ฌธ๊ฑด์ด ๋ฎ์ ์์ผ๋ก ์ ๋ ฌํ์ ๋ ๋์ ํฉ ๊ตฌํ๊ธฐ
select cuisine_type,
restaurant_name,
cnt_order,
sum(cnt_order) over (partition by cuisine_type) sum_cuisine,
sum(cnt_order) over (partition by cuisine_type order by cnt_order) cum_cuisine
from
(
select cuisine_type,
restaurant_name,
count(1) cnt_order
from food_orders
group by 1, 2
) a
order by cuisine_type , cnt_order
date_format(date(date), '%Y') "๋
", // ๋
๋ ์กฐํ
date_format(date(date), '%m') "์", // ์ ์กฐํ
date_format(date(date), '%d') "์ผ", // ์ผ ์กฐํ
date_format(date(date), '%w') "์์ผ" // ์์ผ ์กฐํ