SQL 2주차
짱바쁨.. 그래도 퇴근하고 공부하는 뿌듯함..!!
users를 name 기준으로 묶어줘라
group by 안에 카운트를 사용하면 name으로 묶이는 애들의 숫자를 세줘라
SELECT name, count(*) from users
group by name
네이버 이메일을 사용하는 사람들을 성씨기준으로 정렬
select name,email,count(*) from users
where email like '%@naver%'
group by name
주차별 like 최소로 받은 것
select week,min(likes) from checkins
group by week
주차별 평균 like 수
round는 반올림,소수점 자리수
SELECT week, round(AVG(likes),1) from checkins
group by week
성씨 기준으로 명 수를 세고 많은 순서대로 정렬
select name, count(*) from users
group by name
order by count(*) desc
웹개발 종합반의 결제수단별 주문건수 세어보기
SELECT course_title ,payment_method ,count(*) c from orders
where course_title like '웹개%'
group by payment_method
from → where → group by → select
select -> from -> where -> group by -> order by
자주하는 실수
-> 무엇을 통계내야하는지 명시를 안했기 때문에 대표적으로 1개씩만 보여줌
1 앱개발 종합반의 결제수단별 주문건수 세어보기
select payment_method , count(course_id) from orders
where course_title like '웹개%'
group by payment_method
2 Gmail 을 사용하는 성씨별 회원수 세어보기
SELECT name,count(email) from users
where email like '%@gmail%'
group by name
order by count(email) desc;
3 course_id별 '오늘의 다짐'에 달린 평균 like 개수 구해보기
select course_id , AVG(likes) from checkins c
group by course_id
테이블과 필드명 별칭 지정
SELECT payment_method ,count(*) as cnt from orders o
where o.course_title like '앱개발 종합%'
group by payment_method
숙제: 네이버 이메일을 사용하여 앱개발 종합반을 신청한 주문의 결제수단별 주문건수 세어보기
Select payment_method ,count(*) from orders
where course_title like '앱개%'
and email like '%naver.com'
group by payment_method ```