데이터분석 기록일지

문제풀이/스파르타 - sql 문제

[사전캠프] SQL 연습문제(7)

야하루 2024. 6. 26. 14:08

 

1. 테이블에서 만족도 점수(satisfaction_score)에 따라 피드백을 내림차순으로 정렬하는 쿼리를 작성해주세요!
SELECT *
from practice7
order by satisfaction_score, feedback_date desc

-> 문제가 원하는 바가 뭔지 잘 모르겠어서 이게 맞는지 모르겠다..

satisfaction_score로 우선 정렬하여, 각각의  satisfaction_score에 따른 feedback_date를 내림차순 정렬해서 풀었다.

 

 

2. 테이블에서 각 유저별로 최신 피드백을 찾는 쿼리를 작성해주세요!
SELECT user_name, max(feedback_date) latest_date
from practice7
group by user_name

 

 

3. 테이블에서 만족도 점수가 5점인 피드백의 수를 계산하는 쿼리를 작성해주세요!
SELECT count(1) count_score_5
from practice7
where satisfaction_score = 5

 

 

4. 테이블에서 가장 많은 피드백을 남긴 상위 3명의 고객을 찾는 쿼리를 작성해주세요!

 

-> 구구이 빼고 다 피드백 1개 뿐인데 상위 3명 찾으라길래, count 개수 다음에 이름으로 정렬하는 조건 임의로 추가해줬다. 

SELECT user_name, count(*)
from practice7
group by user_name
order by 2 desc, user_name
limit 3

 

 

+) user_naame만 보이게 하고 싶어서 서브쿼리를 사용해 봤다.

select user_name
from
(
SELECT user_name, count(*) count
from practice7
group by user_name
) a
order by count desc, user_name
limit 3

 

 

5. 테이블에서 평균 만족도 점수가 가장 높은 날짜를 찾는 쿼리를 작성해주세요!
select feedback_date
from 
(
select avg(satisfaction_score) avg_s, feedback_date
from practice7
group by feedback_date
) a
order by avg_s desc
limit 1

 

-> 이번에도 날짜 1개만 딱 보이게 하기 위해서 서브쿼리 사용