본문 바로가기
DB/SQL

[solvesql] 문제 정리

by h_sseul 2023. 7. 19.

- 레스토랑의 일일 매출

 

select day, sum(total_bill) revenue_daily
from tips
group by day
having sum(total_bill) >= 1000
order by revenue_daily desc;

 

 

- 레스토랑의 일일 평균 매출액 계산하기

 

일별 매출 합계 구한 뒤, 이를 하나의 평균 매출액으로 계산하는 것

 

SELECT round(avg(sum_bill), 2) as avg_sales
from 
(SELECT day, sum(total_bill) as sum_bill
from tips
group by day);

 

 

- 우리 플랫폼에 정착한 판매자 2

 

한 명의 판매자 - 여러 명의 고객들(1:N 관계)

 

select seller_id, count(distinct order_id) orders
from olist_order_items_dataset
where price >= 50
group by seller_id
having count(distinct order_id) >= 100
order by orders desc;

 

 

- 많이 주문한 테이블 찾기

 

SELECT *
from tips
where total_bill > (select avg(total_bill) from tips); -- 식사금액 > 평균 식사금액

 

 

- 레스토랑의 대목

 

select * -- 결제내역 모두 출력
from tips
where day in 
	(select day 
    from tips 
    group by day 
    having sum(total_bill) >= 1500);

 

 

- 레스토랑의 요일별 VIP

 

select * -- 모든 컬럼 출력
from tips
where total_bill in -- 요일별 가장 높은 금액
    (select max(total_bill)
    from tips
    group by day);

 

 

- 배송 예정일 예측 성공과 실패

 

SELECT strftime('%Y-%m-%d', order_purchase_timestamp) as purchase_date
  , count(case when order_delivered_customer_date < order_estimated_delivery_date then order_id end) 'success'
  , count(case when order_delivered_customer_date >= order_estimated_delivery_date then order_id end) 'fail'
FROM
  olist_orders_dataset
WHERE
  strftime ('%Y-%m', order_purchase_timestamp) = '2017-01'
  and
  (order_delivered_customer_date or order_estimated_delivery_date) is not null
GROUP BY purchase_date
ORDER BY purchase_date;

 

select 절 case when 구문 익숙해지기

 

 

 

'DB > SQL' 카테고리의 다른 글

[해커랭크] Easy 문제 풀이 1  (0) 2023.07.20
[SQL] Window Function 정리  (0) 2023.06.08
[SQL] WITH절 정리  (0) 2023.06.08
[solvesql] 연습 문제 (난이도 : 쉬움)  (0) 2023.05.23
[Programmers] String, Date 문제 풀이  (0) 2023.04.07