[250507] 사전캠프 3일차 | SQL 사전 퀘스트

곽다혜·2025년 5월 7일
post-thumbnail

SQL 사전 퀘스트

1번
1)
select name, position
from sparta_employees
2)
select disticnt position
from sparta_employees
3)
select
from sparta_employees
where salary between 40000 and 60000
4)
select

from sparta_employees
where hire_date<'2023-01-01'

2번
5)
SELECT product_name, price
from products
6)
select
from products
where product_name like '%프로%'
7)
select

from products
where product_name like '갤%'
8)
select sum(price)
from products

3번
9)
select customer_id
from orders
where amount>=2
10)
select
from orders
where amount>=2
and order_date>'2023-11-02'
11)
select

from orders
where amount<3
and shipping_fee>15000
12)
select *
from orders
order by shipping_fee desc

4번
13)
select name, track
from sparta_students
14)
select
from sparta_students
where track not 'Unity'
15)
select

from sparta_students
where enrollment_year in (2021, 2023)
16)
select enrollment_year
from sparta_students
where track = 'Node.js'
and grade = 'A'

5번
17)
select name
from team_projects
where aws_cost >= 40000
18)
select *
from team_projects
where start_date like '2022%'

SQL 3주차

[특정 문자를 다른 문자로 바꾸기]
바뀐 이름을 한 번에 SQL로 바꿀 수 있다.

replace(바꿀 컬럼, 현재 값, 바꿀 값)

[원하는 문자만 남기기]
전체 데이터가 아닌 특정 문자만 필요할 때, SQL로 필요한 부분만 조회할 수 있다.

substr(조회할 컬럼, 시작 위치, 글자 수)

[여러 컬럼의 문자를 합치기]
원하는 문자가 여러 컬럼에 있을 때, 하나로 합쳐서 업무에 필요한 형태로 만들 수 있다.

concat(붙이고 싶은 값 1, 붙이고 싶은 값 2, 붙이고 싶은 값 3...)

[실습 1- 서울 지역의 음식 타입별 평균 음식 주문금액 구하기]

select substr(addr, 1, 2) "지역명",
cuisine_type,
avg(price) "평균 금액"
from food_orders
where addr like '서울%'
group by 1, 2

[실습 2- 이메일 도메인별 고객 수와 평균 연령 구하기]

select substr(email, 10),
count(1) "고객 수",
ave(age) "평균 연령"
from customers
group by 1

[실습 3-'[지역(시도)]음식점 이름(음식종류)' 컬럼 만들고, 총 주문건수 구하기]

select concat('[', substr(addr, 1, 2), ']' restaurant_name, '(', cuisine_type, ')'] "음식점",
count(1) "주문건수",
from food_orders
group by 1

[조건을 지정해주는 가장 기초 문법 - if 문]
if문은 원하는 조건을 충족할 때 적용할 방법과 아닌 방법을 지정해줄 수 있다.

if(조건, 조건을 충족할 때, 조건을 충족하지 못할 때)

[실습 1- 음식타입 'Korean'은 '한식', 그 외에는 '기타'라고 지정]

select restaurant_name,
cuisine_type "원래 음식 타입",
if(cuisine_type='Korean', '한식', '기타') "음식 타입"
from food_orders

[실습 2-'문곡리'가 평택군에만 해당될 때, 평택 '문곡리'만 '문가리'로 수정]

select addr "원래 주소",
if (addr like '%평택군%', replace(addr, '문곡리', '문가리'), addr) "바뀐 주소)
from food_orders
where addr like '%문곡리%'

[실습 3- 잘못된 이메일 주소만 수정하기]

select substr(if(email like '%gmail%', replace(email, 'gmail', '@gmail'), email), 10) "이메일 도메인",
count(1) "고객 수",
avg(age) "평균 연령"
from customers
group by 1

[조건을 여러가지 지정하고 싶을 때-case 문]
case 문은 각 조건별로 적용할 값을 지정해줄 수 있다.
조건별로 지정을 해주기 때문에 if 문을 여러번 쓴 효과를 낼 수 있다.

case when 조건1 then 값(수식)1
when 조건2 then 값(수식)2
else 값(수식)3 
end

[실습 1]

select restaurant_name,
cuisine_type "원래 음식 타입",
case when cuisine_type='Korean' then '한식'
when cuisine_type in ('Japanese', 'Chinese') then '아시아'
else '기타' end "음식 타입"
from food_orders

[실습 2]

select order_id,
price,
quantity,
case when quantity=1 then price
case when quantity>=2 then price/quantity end "음식 단가"
from food_orders
profile
데이터 분석 적응기

0개의 댓글