ใ์ค๋ ํ์ต ์์ฝใ
1. ๊ฐ์ธ ๊ณต๋ถ
- ์์
๋ณด๋ค ์ฝ๊ณ ๋น ๋ฅธ SQL 3์ฃผ์ฐจ, 4์ฃผ์ฐจ ๊ฐ์ ์๊ฐ
- 3-4. ์กฐ๊ฑด์ ๋ฐ๋ผ ํฌ๋งท์ ๋ค๋ฅด๊ฒ ๋ณ๊ฒฝํด์ผํ๋ค๋ฉด (IF, CASE)
- 3-5. [์ค์ต] SQL๋ก ๊ฐ๋จํ USer Segmentation ํด๋ณด๊ธฐ
- 3-6. [์ค์ต] ์กฐ๊ฑด๋ฌธ์ผ๋ก ์๋ก ๋ค๋ฅธ ์์ ์ ์ฉํ ์์๋ฃ ๊ตฌํด๋ณด๊ธฐ
- 3-7. SQL๋ฌธ์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์๋ฐ ์ ์ค๋ฅ๊ฐ ๋๋์
- 4-1. ํฌ๋งท ๋ณ๊ฒฝ๊ณผ ์กฐ๊ฑด๋ฌธ ๋ณต์ตํ๊ณ , ์ด๋ฒ ์์ ๋ด์ฉ ๋ง๋ณด๊ธฐ
- 4-2. ์ฌ๋ฌ๋ฒ์ ์ฐ์ฐ์ ํ ๋ฒ์ SQL๋ฌธ์ผ๋ก ์ํํ๊ธฐ
- 4-3. [์ค์ต] User Segmentation ์ ์กฐ๊ฑด๋ณ ์์๋ฃ๋ฅผ Subquery๋ก ๊ฒฐํฉํด๋ณด๊ธฐ
- 4-4. [์ค์ต] ๋ณต์กํ ์ฐ์ฐ์ Subquery๋ก ์ํํ๊ธฐ
ใํ์ต ๋ด์ฉ ์ ๋ฆฌใ
1. ์์ ๋ณด๋ค ์ฝ๊ณ ๋น ๋ฅธ SQL 3์ฃผ์ฐจ ๊ฐ์ ์๊ฐ
- ์กฐ๊ฑด์ ๋ฐ๋ผ ํฌ๋งท์ ๋ณ๊ฒฝํ๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ
- โ IF(์กฐ๊ฑด, ์กฐ๊ฑด์ ์ถฉ์กฑํ ๋, ์กฐ๊ฑด์ ์ถฉ์กฑํ์ง ๋ชปํ ๋)
- โก CASE when ์กฐ๊ฑด1 then ๊ฐ(์์)1
when ์กฐ๊ฑด2 then ๊ฐ(์์)2 ...
else ๊ฐ(์์) end
ใใ- ์กฐ๊ฑด์ด 2๊ฐ์ด์์ผ ๊ฒฝ์ฐ์๋ case๋ฌธ์ ์ฌ์ฉํ๋ค.(์กฐ๊ฑด ๊ฐ์ ์ ํ ์์)
ใใ- ์กฐ๊ฑด1, ์กฐ๊ฑด2 ์ธ์ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด else ์๋ต ๊ฐ๋ฅ
ใใ- case๋ก ์์ํด์ end๋ก ๋๋ธ๋ค.
ใใ- when ์กฐ๊ฑด์ ๋์ดํ ๋ ์์์ ๋ถํฐ ํด๋น๋๋ ์กฐ๊ฑด์ ๊ฑธ๋ฌ์ง๋ค.
ใใ- then ๋ค์ ๊ฐ ํน์ if๋ฌธ๊ณผ ๊ฐ์ ์์๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
select case when score>=90 then 'A'
when score>=80 then 'B'
when score>=70 then 'C'
else 'D' end
#์ ์ฟผ๋ฆฌ์ ๊ฒฝ์ฐ ๋๋ฒ์งธ ์กฐ๊ฑด์์ ์ด๋ฏธ scroe>=90์ ๋ฐ์ดํฐ๋ ํํฐ๋ง๋๋ค.
#์ฆ B์ ๊ฐ์ ๊ฐ์ง ๋ฐ์ดํฐ๋ score<90 and score>=80๋ฅผ ์๋ฏธํ๋ค.
- ์กฐ๊ฑด์ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ
- ์๋ก์ด ์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋ง๋ค๋ (์กฐ๊ฑด์ ๋ฐ๋ฅธ ๊ทธ๋ฃนํ)
- ์กฐ๊ฑด๋ณ๋ก ๋ค๋ฅธ ์ฐ์ฐ ์กฐ๊ฑด์ ์ง์ ํ ๋
- ๋ค๋ฅธ ๋ฌธ๋ฒ ์์์ ์ ์ฉํ ๋
- 3-5๊ฐ ์ค์ต
#๋ด๊ฐ ์ด ์
select case when age<20 and gender='male' then '10๋ ๋จ์ฑ'
when age>=20 and gender='male' then '20๋ ๋จ์ฑ'
when age<20 and gender='female' then '10๋ ์ฌ์ฑ'
when age>=20 and gender='female' then '20๋ ์ฌ์ฑ'
end "๊ตฌ๋ถ", name,
age, gender
from customers
where age between 10 and 30
#์ ๋ต(๊ฐ์์์ ์ ๊ณตํ ๋ต)
select 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 "๊ณ ๊ฐ ๋ถ๋ฅ",
name,
age,
gender
from customers
where age between 10 and 29
#์ค๋ต๋
ธํธ
where์ ์ between ๋ฒ์ ์๋ชป ์ง์
- Data Type์ ๋ฐ๋ฅธ ์ค๋ฅ
- SQL๋ฌธ์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์๋ฐ 'data type'์ด๋ผ๋ ๋จ์ด์ ํจ๊ป ์ค๋ฅ๊ฐ ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
- ๋ฐ์ดํฐ ํ์ ์ด ๋ฌธ์ํ์ธ๋ฐ ์ซ์๋ก ์คํ๋จํ๊ณ ์ฐ์ฐ์ ์ ์ฉํ ๋ ๋ฐ์ํ๋ ์ค๋ฅ์ด๋ค.
- ๋ณด๊ธฐ์๋ 1,2,3๊ณผ ๊ฐ์ ์ซ์์ฌ๋ ๋ฐ์ดํฐ ํ์ ์ด ๋ฌธ์ํ์ผ ์ ์๋ค. (๋ฐ์ดํฐ ํ์ ํ์ธ ํ์)
- dbeaver์์๋ ์ปฌ๋ผ ์ข์ธก์ a-z๋๋ 123์ผ๋ก ๋ฐ์ดํฐ ํ์ ์ด ํ๊ธฐ๋์ด์๋ค.
- ๋ฐ์ดํฐ ํ์
์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ : Cast(์ปฌ๋ผ as ๋ฌธ์ํ์)
- ์ปฌ๋ผ์ ํฌํจ๋ ๊ธ์๋ฅผ ์ซ์๋ก ๋ณ๊ฒฝ
Cast(if(์ปฌ๋ผ='๊ธ์', '1(์์์ ์ซ์)', ์ปฌ๋ผ) as decimal) - ์ซ์๋ฅผ ๋ฌธ์๋ก ๋ณ๊ฒฝ
Cast(์ปฌ๋ผ as char)
- ์ปฌ๋ผ์ ํฌํจ๋ ๊ธ์๋ฅผ ์ซ์๋ก ๋ณ๊ฒฝ
2. ์์ ๋ณด๋ค ์ฝ๊ณ ๋น ๋ฅธ SQL 4์ฃผ์ฐจ ๊ฐ์ ์๊ฐ
- Subquery()
- ์ฉ๋
ใใ- ์ฌ๋ฌ๋ฒ์ ์ฐ์ฐ์ ์ํํด์ผ ํ ๋
ใใ- ์กฐ๊ฑด๋ฌธ์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ ๋
ใใ- ์กฐ๊ฑด์ Query ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ ๋ - select column1, special_column
from
( /* subquery */
select column1, column2 special_column
from table1
) a - ์์์ผ๋ก ๋ณด๋ฉด ์ด๋ ต์ง๋ง, from ๋ค์ ํ ์ด๋ธ์ด ์๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์, ์์ ์ฟผ๋ฆฌ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค.
- a์ ๊ฐ์ด ๋ณ์นญ์ ๋ถ์ด๋ ๊ฒ์ด ํ์๋ ์๋์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ๋ถ์ด๋ ๊ฑธ ๊ถ์ฅํ๋ค.
์๋ํ๋ฉด SQL ๋ฌธ๋ฒ์์๋ ์๋ธ์ฟผ๋ฆฌ๊ฐ ํ๋์ ํ ์ด๋ธ์ฒ๋ผ ์ทจ๊ธ๋๊ธฐ ๋๋ฌธ์ ์ด๋ฆ์ ์ง์ ํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฐธ์กฐํ ์ ์๊ฒ ํด์ผํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ณ์นญ์ ๋ถ์ด์ง ์์ผ๋ฉด ์ผ๋ถ DBMS์์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์๋ค. (a์ ๊ฐ์ ์์์ ๋ณ์นญ์ ๋ถ์ฌ๋์!)
- ์ฉ๋
#๋ด๊ฐ ์ด ์
select restaurant_name,
order_total,
price_total,
case when order_total <= 5 then 0.1
when order_total > 15 and price_total >= 30000 then 0.005
else 0.01 end rate
from
(select restaurant_name,
count(quantity) order_total,
sum(price) price_total
from food_orders
group by 1) a
#์ ๋ต(๊ฐ์์์ ์ ๊ณตํ ๋ต)
select restaurant_name,
sum_price,
sum_quantity,
case when sum_quantity <= 5 then 0.1
when sum_quantity > 15 and sum_price >= 300000 then 0.005
else 0.01 end rate
from
(select restaurant_name,
sum(price) sum_price,
sum(quantity) sum_quantity
from food_orders
group by 1) a
#์ค๋ต๋
ธํธ
1.๋ฌธ์ ๋ ์์์ ์ ์ด ์ฃผ๋ฌธ์๋๊ณผ ์ฃผ๋ฌธ ๊ธ์ก์ ์ฐ์ฐํ๋ ๊ฒ์ด์๋๋ฐ,
์ฃผ๋ฌธ์๋์ ๊ณ์ฐํ ๋ sum()์ด ์๋ count()๋ก ์ง๊ณํ์ฌ ๋ฐ์ดํฐ ์ผ๋ถ๋ฅผ ๋๋ฝ์ํด...
2.์ซ์๋ 300,000 ์ด์์ธ๋ฐ 30,000์ผ๋ก ์๋ชป ์ ์๋ค..
ใ๋ฉ๋ชจใ
1. between๋ฒ์ ์ฃผ์ํ๊ธฐ
ใ- between 1 AND 30์ผ ๊ฒฝ์ฐ 1.0๋ถํฐ 30.0๊น์ง ํฌํจํ๋ค. ใ- 20๋๋ฅผ ํํฐ๋งํ๋ค๋ฉด between 20 AND 29๋ก ์ง์ ํด์ผํ๋ค.
2. count()์ sum() ํท๊ฐ๋ฆฌ์ง ์๊ธฐ, ์กฐ๊ฑด ์ ์ฝ๊ณ ์ซ์ ์ค์ํ์ง ์๊ธฐ
ใ- ๋ฌธ์ ๋ฅผ ์ ์ฝ๊ณ ์ฃผ์ํ์!
3. 1on1 ์งํํ๋ค. ์ฒซ๋ฒ์งธ๋ก!!
ใ๋ค์ ๊ณต๋ถ ๊ณํใ
1. 5์ฃผ์ฐจ ๊ฐ์ ์ง์ง์ง์ง ๋ง๋ฌด๋ฆฌ
'Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL 01/21_์ฌ์ ์บ ํ 7์ผ์ฐจ] SQL ๊ธฐ์ด ๋ฌธ๋ฒ (0) | 2025.01.21 |
---|---|
[TIL 01/20_์ฌ์ ์บ ํ 6์ผ์ฐจ] SQL ๊ธฐ์ด ๋ฌธ๋ฒ (0) | 2025.01.20 |
[TIL 01/16_์ฌ์ ์บ ํ 4์ผ์ฐจ] SQL ๊ธฐ์ด ๋ฌธ๋ฒ (0) | 2025.01.16 |
[TIL 01/15_์ฌ์ ์บ ํ 3์ผ์ฐจ] SQL ๊ธฐ์ด ๋ฌธ๋ฒ (0) | 2025.01.15 |
[TIL 01/14_์ฌ์ ์บ ํ 2์ผ์ฐจ] SQL ๊ธฐ์ด ๋ฌธ๋ฒ (1) | 2025.01.14 |