엑셀보다 쉬운 SQL - 2주차(Group by, Order by)

dlfpire·2023년 3월 23일

스파르타코딩클럽

목록 보기
2/4

<Group by : 그룹>
group by 안에서 count를 사용하면 묶이는 것들의 갯수를 세준다

select name, count(*) from users
group by name

sum(), min(최소값을 알고 싶은 필드명), max(), avg()

<Order by : 정렬>

select name, count(*) from users
group by name
order by count(*)

*desc : 내림차순

<Where와 함께 사용하기>

select payment_method, count(*) from orders
where course_title = '웹개발 종합반'
group by payment_method
order by count(*) 

<퀴즈>
1. 앱개발 종합반의 결제수단별 주문건수 세어보기

select payment_method, count(*) from orders
where course_title = '앱개발 종합반'
group by payment_method
  1. Gmail을 사용하는 성씨별 회원 수 세어보기
select name, count(*) from users
where email like '%gmail.com'
group by name
  1. course_id 별 오늘의 코멘트
select course_id, avg(likes) from checkins
group by course_id

<별칭>

select payment_method, count(*) as cnt from orders o
where o.course_title = '앱개발 종합반'
group by payment_method 

<과제>
1. 네이버 이메일을 사용하여 앱개발 종합반을 신청한 주문의 결제수단별 주문건수 세어보기

SELECT payment_method, count(*) FROM orders
WHERE email like '%naver.com' and course_title = '앱개발 종합반'
group by payment_method

0개의 댓글