๐ก ๋ชจ๋ ํ ๊ธ์ ์ด๊ณ ๋ซ๋ ๋จ์ถํค
Windows : Ctrl + alt + t
Mac : โ + โฅ + t
โ๏ธ SQL ๊ธฐ๋ณธ๊ตฌ์กฐ๋ฅผ ๋ณต์ตํ๊ณ , ์ด๋ฒ ์์ ์์ ๋ฐฐ์ธ ๋ด์ฉ์ ์์๋ด ์๋ค
select
from
where
group by
order byโ๏ธ Query ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋๋ก ๋ฌธ์ ๋ฐ์ดํฐ์ ํํ๋ฅผ ๋ฐ๊ฟ๋ด ์๋ค
๐ก ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ค๋ณด๋ฉด, Query ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ์ด์ฉํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด์.
์๋ง ์ค์ต์ ํ๋ฉด์ ์๋์ ๊ฒฝ์ฐ๋ฅผ ํ ๋ฒ์ฏค์ ์๊ฐํด๋ดค์ ํ
๋ฐ์, ํ ๋ฒ ๊ฐ๊ฐ์ ์ผ์ด์ค์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์์๋ด
์๋ค
- ๐ ๋ฐ๋ ์์ ์ด๋ฆ, ์ง์ญ ์ด๋ฆ ํ ๋ฒ์ SQL ๋ก ๋ฐ๊ฟ ์ ์์ต๋๋ค
- SQL ์์๋ ํน์ ๋ฌธ์๋ฅผ ๋ค๋ฅธ ๊ฒ์ผ๋ก ๋ฐ๊ฟ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- ์์1) ์ต๊ทผ์ ์์ ์ด๋ฆ์ด ๋ฐ๋์์ง๋ง ๊ณผ๊ฑฐ ๋ฐ์ดํฐ์๋ ์๋ ์ด๋ฆ์ผ๋ก ์ ์ฅ๋์ด์์ด์
- ์์2) ์์ ์ โ๋ฌธ๊ณก๋ฆฌโ ๋ผ๋ ์ง๋ช
์ด โ๋ฌธ๊ฐ๋ฆฌโ ๋ก ๋ฐ๋์์ด์
- ํจ์๋ช
: replace
- ์ฌ์ฉ ๋ฐฉ๋ฒ
replace(๋ฐ๊ฟ ์ปฌ๋ผ, ํ์ฌ ๊ฐ, ๋ฐ๊ฟ ๊ฐ)
[์ค์ต1] ์๋น ๋ช ์ โBlue Ribbonโ ์ โPink Ribbonโ ์ผ๋ก ๋ฐ๊พธ๊ธฐ
- [์ฝ๋์ค๋ํซ] Replace ์์
<select restaurant_name "์๋ ์์ ๋ช ", replace(restaurant_name, 'Blue', 'Pink') "๋ฐ๋ ์์ ๋ช " from food_orders where restaurant_name like '%Blue Ribbon%'
[์ค์ต2] ์ฃผ์์ โ๋ฌธ๊ณก๋ฆฌโ ๋ฅผ โ๋ฌธ๊ฐ๋ฆฌโ ๋ก ๋ฐ๊พธ๊ธฐ
- [์ฝ๋์ค๋ํซ] Replace ์์
select addr "์๋ ์ฃผ์", replace(addr, '๋ฌธ๊ณก๋ฆฌ', '๋ฌธ๊ฐ๋ฆฌ') "๋ฐ๋ ์ฃผ์" from food_orders where addr like '%๋ฌธ๊ณก๋ฆฌ%'
substr(์กฐํ ํ ์ปฌ๋ผ, ์์ ์์น, ๊ธ์ ์)
[์ค์ต] (์์ธ ์์์ ๋ค์ ์ฃผ์๋ฅผ ์ ์ฒด๊ฐ ์๋ โ์๋โ ๋ง ๋์ค๋๋ก ์์ )
- [์ฝ๋์ค๋ํซ] substring ์์
select addr "์๋ ์ฃผ์", substr(addr, 1, 2) "์๋" from food_orders where addr like '%์์ธํน๋ณ์%'
concat(๋ถ์ด๊ณ ์ถ์ ๊ฐ1, ๋ถ์ด๊ณ ์ถ์ ๊ฐ2, ๋ถ์ด๊ณ ์ถ์ ๊ฐ3, .....)
[์ค์ต] ์์ธ์์ ์๋ ์์์ ์ โ[์์ธ] ์์์ ๋ช โ ์ด๋ผ๊ณ ์์
- [์ฝ๋์ค๋ํซ] Concat ์์
select restaurant_name "์๋ ์ด๋ฆ", addr "์๋ ์ฃผ์", concat('[', substring(addr, 1, 2), '] ', restaurant_name) "๋ฐ๋ ์ด๋ฆ" from food_orders where addr like '%์์ธ%'
โ๏ธ ๋ฌธ์ ๋ฐ์ดํฐ ๋ณ๊ฒฝ๊ณผ Group by ์ ์ ํ ๋ฒ์ ์ฌ์ฉํด๋ด ์๋ค
- 1. Query ๋ฅผ ์ ๊ธฐ ์ ์ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด๊ธฐ
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ
- 1.Query ๋ฅผ ์ ๊ธฐ ์ ์ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด๊ธฐ (ํด๋ต)
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ โ ์ฃผ๋ฌธ ํ
์ด๋ธ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ โ ์ฃผ๋ฌธ ๊ธ์ก, ์์ ํ์
, ์ฃผ์
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ โ ์์ธ ์ง์ญ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ โ ํ๊ท ๊ตฌํ๋ ์์, ํน์ ๋ฌธ์๋ง ๋ฝ๋ ๊ธฐ๋ฅ
- 2.๊ตฌ๋ฌธ์ผ๋ก ๋ง๋ค๊ธฐ
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ
- 2.๊ตฌ๋ฌธ์ผ๋ก ๋ง๋ค๊ธฐ (ํด๋ต)
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ โ from food_orders
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ โ price, cuisine_type, addr
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ โ where addr like โ%์์ธ%โ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ โ avg(price), substring(addr, 1, 2)
- 3.์ ์ฒด ๊ตฌ์กฐ๋ก ํฉ์น๊ธฐ
select substring(addr, 1, 2) "์๋",
cuisine_type "์์ ์ข
๋ฅ",
avg(price) "ํ๊ท ๊ธ์ก"
from food_orders
where addr like '%์์ธ%'
group by 1, 2

select substring(email, 10) "๋๋ฉ์ธ",
count(customer_id) "๊ณ ๊ฐ ์",
avg(age) "ํ๊ท ์ฐ๋ น"
from customers
group by 1
select concat('[', substring(addr, 1, 2), '] ', restaurant_name, ' (', cuisine_type, ')') "๋ฐ๋์ด๋ฆ",
count(1) "์ฃผ๋ฌธ๊ฑด์"
from food_orders
group by 1
โ๏ธ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ์ฐ์ฐ์ ํ๋ ๋ฐฉ๋ฒ์ ์์๋ด ์๋ค
๐ ๋ฒ์ฃผ๋ณ๋ก ๊ฐ์ ๊ตฌํ ๋๋ group by ๋ฅผ ์ป์ฃ .
๋ฒ์ฃผ๋ณ๋ก ๋ค๋ฅธ ์ฐ์ฐ (๊ณ์ฐ, ๋ฌธ์ ๋ฐ๊พธ๊ธฐ) ์ ์ ์ฉํ ์๋ ์์๊น์?
๐ SQL ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฐ์ฐ์ ์ ์ฉํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค
โ๋ด๊ฐ ์ํ๋ ๋ฒ์ฃผโ ๋ฅผ ์กฐ๊ฑด์ผ๋ก ์ฃผ๊ณ , ํด๋น ๋ฒ์ฃผ์ ์ ์ฉํ๊ณ ์ถ์ ๊ฒ์ ์ง์ ํด ์ฃผ๋ ๋ฐฉ์์
๋๋ค
๐ก ๊ฐ๋
์ ์ดํดํ๊ธฐ ์ด๋ ต๋ค๋ฉด ์๋์ ์์๋ฅผ ์ฐธ๊ณ ํด๋ด
์๋ค
- ์์ ํ์
์ โKoreanโ ์ผ ๋๋ โํ์โ, โKoreanโ ์ด ์๋ ๊ฒฝ์ฐ์๋ โ๊ธฐํโ ๋ผ๊ณ ์ง์ ํ๊ณ ์ถ์ด์
- ์ฃผ์์ ์๋๋ฅผ โ๊ฒฝ๊ธฐ๋โ ์ผ๋๋ โ๊ฒฝ๊ธฐ๋โ, ์๋ ๋๋ ์์ ๋ ๊ธ์๋ง ์ฌ์ฉํ๊ณ ์ถ์ด์
- ์์ ๋จ๊ฐ๋ฅผ ์ฃผ๋ฌธ ์๋์ด 1์ผ ๋๋ ์์ ๊ฐ๊ฒฉ, ์ฃผ๋ฌธ ์๋์ด 2๊ฐ ์ด์์ผ ๋๋ ์์๊ฐ๊ฒฉ/์ฃผ๋ฌธ์๋ ์ผ๋ก ์ง์ ํ๊ณ ์ถ์ด์
๐ ์กฐ๊ฑด์ ์ง์ ํด์ฃผ๋ ๊ฐ์ฅ ๊ธฐ์ด ๋ฌธ๋ฒ์ โIfโ ๋ฌธ์
๋๋ค (์์
์ ๊ธฐ๋ฅ๊ณผ ์ ์ฌํฉ๋๋ค)
- IF ๋ฌธ์ ์ํ๋ ์กฐ๊ฑด์ ์ถฉ์กฑํ ๋ ์ ์ฉํ ๋ฐฉ๋ฒ๊ณผ ์๋ ๋ฐฉ๋ฒ์ ์ง์ ํด ์ค ์ ์์ต๋๋ค
- ์์) ์์ ํ์
์ โKoreanโ ์ผ ๋๋ โํ์โ, โKoreanโ ์ด ์๋ ๊ฒฝ์ฐ์๋ โ๊ธฐํโ ๋ผ๊ณ ์ง์ ํ๊ณ ์ถ์ด์
- ํจ์๋ช
: if
- ์ฌ์ฉ ๋ฐฉ๋ฒ
```sql
if(์กฐ๊ฑด, ์กฐ๊ฑด์ ์ถฉ์กฑํ ๋, ์กฐ๊ฑด์ ์ถฉ์กฑํ์ง ๋ชปํ ๋)
```
[์ค์ต1] ์์ ํ์ ์ โKoreanโ ์ผ ๋๋ โํ์โ, โKoreanโ ์ด ์๋ ๊ฒฝ์ฐ์๋ โ๊ธฐํโ ๋ผ๊ณ ์ง์
- [์ฝ๋์ค๋ํซ] If๋ฌธ ์์
select restaurant_name, cuisine_type "์๋ ์์ ํ์ ", if(cuisine_type='Korean', 'ํ์', '๊ธฐํ') "์์ ํ์ " from food_orders
[์ค์ต2] 02. ๋ฒ ์ค์ต์์ โ๋ฌธ๊ณก๋ฆฌโ ๊ฐ ํํ์๋ง ํด๋น๋ ๋, ํํ โ๋ฌธ๊ณก๋ฆฌโ ๋ง โ๋ฌธ๊ฐ๋ฆฌโ ๋ก ์์
- [์ฝ๋์ค๋ํซ] If๋ฌธ ์์2
select addr "์๋ ์ฃผ์", if(addr like '%ํํ๊ตฐ%', replace(addr, '๋ฌธ๊ณก๋ฆฌ', '๋ฌธ๊ฐ๋ฆฌ'), addr) "๋ฐ๋ ์ฃผ์" from food_orders where addr like '%๋ฌธ๊ณก๋ฆฌ%'
[์ค์ต3] 03. ๋ฒ ์ค์ต์์ ์๋ชป๋ ์ด๋ฉ์ผ ์ฃผ์ (gmail) ๋ง ์์ ์ ํด์ ์ฌ์ฉ
- [์ฝ๋์ค๋ํซ] If๋ฌธ ์์3
select substring(if(email like '%gmail%', replace(email, 'gmail', '@gmail'), email), 10) "์ด๋ฉ์ผ ๋๋ฉ์ธ", count(customer_id) "๊ณ ๊ฐ ์", avg(age) "ํ๊ท ์ฐ๋ น" from customers group by 1
case when ์กฐ๊ฑด1 then ๊ฐ(์์)1
when ์กฐ๊ฑด2 then ๊ฐ(์์)2
else ๊ฐ(์์)3
end[์ค์ต1] ์์ ํ์ ์ โKoreanโ ์ผ ๋๋ โํ์โ, โJapaneseโ ํน์ โChieneseโ ์ผ ๋๋ โ์์์โ, ๊ทธ ์ธ์๋ โ๊ธฐํโ ๋ผ๊ณ ์ง์
select restaurant_name, cuisine_type AS "์๋ ์์ ํ์ ", case when (cuisine_type='Korean') then 'ํ์' else '๊ธฐํ' end as " ์์ ํ์ " from food_orders
[์ค์ต2] ์์ ๋จ๊ฐ๋ฅผ ์ฃผ๋ฌธ ์๋์ด 1์ผ ๋๋ ์์ ๊ฐ๊ฒฉ, ์ฃผ๋ฌธ ์๋์ด 2๊ฐ ์ด์์ผ ๋๋ ์์๊ฐ๊ฒฉ/์ฃผ๋ฌธ์๋ ์ผ๋ก ์ง์
*์์ ๊ฐ์ ์์ if ๋ก๋ ์ธ ์ ์์ฃ ! ํ ๋ฒ ์ฐ์ตํด๋ณด์๋ ๊ฒ์ ์ถ์ฒํฉ๋๋ค!
- [์ฝ๋์ค๋ํซ] Case When ์ค์ต1
select order_id, price, quantity, case when quantity=1 then price when quantity>=2 then price/quantity end "์์ ๋จ๊ฐ" from food_orders
[์ค์ต3] ์ฃผ์์ ์๋๋ฅผ โ๊ฒฝ๊ธฐ๋โ ์ผ๋๋ โ๊ฒฝ๊ธฐ๋โ, โํน๋ณ์โ ํน์ โ๊ด์ญ์โ ์ผ ๋๋ ๋ถ์ฌ์, ์๋ ๋๋ ์์ ๋ ๊ธ์๋ง ์ฌ์ฉ
- [์ฝ๋์ค๋ํซ] Case When ์ค์ต2
select restaurant_name, addr, case when addr like '%๊ฒฝ๊ธฐ๋%' then '๊ฒฝ๊ธฐ๋' when addr like '%ํน๋ณ%' or addr like '%๊ด์ญ%' then substring(addr, 1, 5) else substring(addr, 1, 2) end "๋ณ๊ฒฝ๋ ์ฃผ์" from food_orders
ํ๊ตญ ์์, ์์์ ์์, ๋ฏธ๊ตญ ์์, ์ ๋ฝ ์์ ์ด๋ฐ ์์ ์๋ก์ด cuisine_category ๋ฅผ ์์ฑํ ์ ์์ฃ 10๋ ์ฌ์ฑ, 10๋ ๋จ์ฑ, 20๋ ์ฌ์ฑ, 20๋ ๋จ์ฑ ๋ฑ, ์ด๋ฐ ์์ ์ฑ๋ณ๊ณผ ๋์ด๋ณ๋ก ์๋ก์ด ๊ณ ๊ฐ ๊ตฐ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ์์ฑํ ์ ์์ฃ โ๏ธ ์๋ก์ด ์นดํ ๊ณ ๋ฆฌ ๋ง๋ค๊ธฐ - ์กฐ๊ฑด๋ฌธ๊ณผ ์์์ ์ด์ฉํ์ฌ ๊ฐ๋จํ User Segmentation ์ ํด๋ด ์๋ค
- 1. Query ๋ฅผ ์ ๊ธฐ ์ ์ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด๊ธฐ
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ
- 1.Query ๋ฅผ ์ ๊ธฐ ์ ์ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด๊ธฐ (ํด๋ต)
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ โ ๊ณ ๊ฐ ํ
์ด๋ธ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ โ ์ด๋ฆ, ๋์ด, ์ฑ๋ณ
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ โ ๋์ด๊ฐ 10์ธ ์ด์, 30์ธ ๋ฏธ๋ง
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ โ ์กฐ๊ฑด๋ฌธ
- 2.๊ตฌ๋ฌธ์ผ๋ก ๋ง๋ค๊ธฐ
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ
- 2.๊ตฌ๋ฌธ์ผ๋ก ๋ง๋ค๊ธฐ (ํด๋ต)
1. ์ด๋ค ํ
์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ๊ฒ์ธ๊ฐ โ from customers
2. ์ด๋ค ์ปฌ๋ผ์ ์ด์ฉํ ๊ฒ์ธ๊ฐ โ name, age, gender
3. ์ด๋ค ์กฐ๊ฑด์ ์ง์ ํด์ผ ํ๋๊ฐ โ where age between 10 and 29
4. ์ด๋ค ํจ์ (์์) ์ ์ด์ฉํด์ผ ํ๋๊ฐ โ case when โฆ. end
- 3.์ ์ฒด ๊ตฌ์กฐ๋ก ํฉ์น๊ธฐ
select name,
age,
gender,
case when (age between 10 and 19) and gender='male' then "10๋ ๋จ์"
when (age between 10 and 19) and gender='female' then "10๋ ์ฌ์"
when (age between 20 and 29) and gender='male' then "20๋ ๋จ์"
when (age between 20 and 29) and gender='female' then "20๋ ์ฌ์" end "๊ทธ๋ฃน"
from customers
where age between 10 and 29

(Korean = ํ์
Japanese, Chinese, Thai, Vietnamese, Indian = ์์์์
๊ทธ์ธ = ๊ธฐํ)
(๊ฐ๊ฒฉ = 5000, 15000, ๊ทธ ์ด์)
select restaurant_name,
price/quantity "๋จ๊ฐ",
cuisine_type,
order_id,
case when (price/quantity <5000) and cuisine_type='Korean' then 'ํ์1'
when (price/quantity between 5000 and 15000) and cuisine_type='Korean' then 'ํ์2'
when (price/quantity > 15000) and cuisine_type='Korean' then 'ํ์3'
when (price/quantity <5000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '์์์์1'
when (price/quantity between 5000 and 15000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '์์์์2'
when (price/quantity > 15000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '์์์์3'
when (price/quantity <5000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '๊ธฐํ1'
when (price/quantity between 5000 and 15000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '๊ธฐํ2'
when (price/quantity > 15000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '๊ธฐํ3' end "์๋น ๊ทธ๋ฃน"
from food_orders

โ๏ธ ์กฐ๊ฑด๋ฌธ์ ์ด์ฉํ์ฌ ๋ค๋ฅธ ์์์ ์ ์ฉํด๋ณด๊ธฐ
(์ง์ญ : ์์ธ, ๊ธฐํ - ์์ธ์ผ ๋๋ ์์๋ฃ ๊ณ์ฐ * 1.1, ๊ธฐํ์ผ ๋๋ ๊ณฑํ๋ ๊ฐ ์์
์๊ฐ : 25๋ถ, 30๋ถ - 25๋ถ ์ด๊ณผํ๋ฉด ์์ ๊ฐ๊ฒฉ์ 5%, 30๋ถ ์ด๊ณผํ๋ฉด ์์ ๊ฐ๊ฒฉ์ 10%)
```sql
select restaurant_name,
order_id,
delivery_time,
price,
addr,
case when delivery_time>25 and delivery_time<=30 then price*0.05*(if(addr like '%์์ธ%', 1.1, 1))
when delivery_time>30 then price*1.1*(if(addr like '%์์ธ%', 1.1, 1))
else 0 end "์์๋ฃ"
from food_orders
```
(์ฃผ๋ฌธ ์๊ธฐ : ํ์ผ ๊ธฐ๋ณธ๋ฃ = 3000 / ์ฃผ๋ง ๊ธฐ๋ณธ๋ฃ = 3500
์์ ์ : 3๊ฐ ์ดํ์ด๋ฉด ํ ์ฆ ์์ / 3๊ฐ ์ด๊ณผ์ด๋ฉด ๊ธฐ๋ณธ๋ฃ * 1.2)
select order_id,
price,
quantity,
day_of_the_week,
if(day_of_the_week='Weekday', 3000, 3500)*(if(quantity<=3, 1, 1.2)) "ํ ์ฆ๋ฃ"
from food_orders

โ๏ธ ์ซ์ ๊ณ์ฐ์ด๋ ๋ฌธ์ ๊ฐ๊ณต ์ ์์ฃผ ๋ฐ์ํ๋ ์ค๋ฅ๋ฅผ ์์๋ด ์๋ค.

- ๋ฐ๋ผ์ ๋ฌธ์, ์ซ์๋ฅผ ํผํฉํ์ฌ ํจ์์ ์ฌ์ฉ ํ ๋์๋ ๋ฐ์ดํฐ ํ์
์ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํฉ๋๋ค
--์ซ์๋ก ๋ณ๊ฒฝ
cast(if(rating='Not given', '1', rating) as decimal)
--๋ฌธ์๋ก ๋ณ๊ฒฝ
concat(restaurant_name, '-', cast(order_id as char))
๐โโ๏ธ ๋ค์์ ์กฐ๊ฑด์ผ๋ก ๋ฐฐ๋ฌ์๊ฐ์ด ๋ฆ์๋์ง ํ๋จํ๋ ๊ฐ์ ๋ง๋ค์ด์ฃผ์ธ์.
์ฃผ์ค : 25๋ถ ์ด์
์ฃผ๋ง : 30๋ถ ์ด์
[์ฝ๋์ค๋ํซ] HW3 ์ ๋ต์ฝ๋
select order_id,
restaurant_name,
day_of_the_week,
delivery_time,
case when day_of_the_week='Weekday' and delivery_time>=25 then 'Late'
when day_of_the_week='Weekend' and delivery_time>=30 then 'Late'
else 'On-time' end "์ง์ฐ์ฌ๋ถ"
from food_orders