1. SQL5주차 완강 및 복습, 아티클스터디(데이터 분석가가 되어보니 중요한 것들)
2. 나만의 언어로 학습
3. 오늘 해본 문제들 코드모음
[실습] pivot table 뷰 만들어보기(성별, 연령별 주문건수- 나이는10-59사이, 연령순 내림차순)
select age,
max(if(gender='male', cnt_order, 0)) "male",
max(if(gender='female', cnt_order, 0)) "female"
from
(
select gender,
case when age between 10 and 19 then 10
when age between 20 and 29 then 20
when age between 30 and 39 then 30
when age between 40 and 49 then 40
when age between 50 and 59 then 50 end age,
count(1) cnt_order
from food_orders f inner join customers c on f.customer_id=c.customer_id
where age between 10 and 59
group by 1,2
) a
group by 1
order by 1 desc
[실습] Rank함수를 이용하여 음식타입별 순위를 3위까지만 데이터 추출
select cuisine_type, restaurant_name,cnt_order,
rank() over(partition by cuisine_type order by cnt_order desc) ranking
from
(
select cuisine_type, restaurant_name,cnt_order,
rank() over(partition by cuisine_type order by cnt_order desc) ranking
from
(
select cuisine_type , restaurant_name,
count(1) cnt_order
from food_orders
group by 1, 2
) a
) b
where ranking<=3
[실습] 주문건이 해당하는 음식 타입이 차지하는 비율을 구하고, 주문건 낮은 순으로정렬시 누적합
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
[실습] 년도별로 3월의 주문건수 구하기
select date_format(date(date),'%Y')"년",
date_format(date(date), '%m') "월",
date_format(date(date),'%Y%m')"년월",
count(1) "주문건수"
from food_orders f inner join payments p on f.order_id=p.order_id
where date_format(date(date), '%m') = '03'
group by 1, 2, 3
order by 1
[숙제] 음식 타입별, 연령별 주문건수 pivot view 만들기(연령은 10~59세 사이)
select cuisine_type,
max(if(age=10, cnt_order, 0))"10대",
max(if(age=20, cnt_order, 0)) "20대",
max(if(age=30, cnt_order,0))" 30대",
max(if(age=40, cnt_order, 0)) "40대",
max(if(age=50, cnt_order, 0)) "50대"
from
(
select f.cuisine_type,
case when age between 10 and 19 then 10
when age between 20 and 29 then 20
when age between 30 and 39 then 30
when age between 40 and 49 then 40
when age between 50 and 59 then 50 end age,
count(1) cnt_order
from food_orders f inner join customers c on f.customer_id=c.customer_id
where age between 10 and 59
group by 1, 2
)a
group by 1
select name
from team_projects
where aws_cost>=40000
team_projects 테이블에서 2022년에 시작된 프로젝트를 선택하는 쿼리를 작성해주세요! 단, start_date < ‘2023-01-01’ 조건을 사용하지 말고 쿼리를 작성해주세요!
(내가 생각한 답)
select
from team_projects
where start_date between '2022-01-01' and '2022-12-31'
(실제 정답)
select
from team_projects
where YEAR(star_date)=2022
team_projects 테이블에서 현재 진행중인 프로젝트를 선택하는 쿼리를 작성해주세요. 단, 지금 시점의 날짜를 하드코딩해서 쿼리하지 말아주세요!
select *
from team_projects
where CURDATE() between start_date and end_dat
team_projects 테이블에서 각 프로젝트의 지속 기간을 일 수로 계산하는 쿼리를 작성해주세요!
select name, DATEDIFF(end_date,start_date) AS working_day
from team_projects
4. SQLD 자격증 강의 수강