SQL : group by 와 order by (2)

Stian·2023년 6월 26일

group by와 order by를 이용한 문제 해결

select * from checkins
where week=2

select payment_method, count(*) from orders
group by payment_method

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

#desc써야 내림차순된다구요.

select * from users
order by name desc

#이름이 황씨부터 나오게 됨. 만약 order by 를 시간 열에다가 지정한다면,,, 시간 순서대로 정렬이 된다는 얘기임. 

#group by 연습하기 퀴즈 1. 앱개발 종하반의 결제수단별 주문건수 세어보기

select payment_method, count(*) from orders
group by payment_method

#group by 연습하기 퀴즈 2. gmail 성씨별 회원수 세보기.

select name, count(*) from users
where email like '%@gmail.com'

#순서에 유의하기
group by name

#course id별 오늘의 다짐에 달린 평균 like 개수. 반올림한다면?

select course_id, round(avg(likes)) from checkins
group by course_id

select * from orders o #이제 orders를 o라고 부르겠다.
where o.course_title = '앱개발 종합반'

#o에서의 course_title를 보겠다.

select payment_method, count() as cnt from orders o
group by payment_method
order by count(
) desc

#as 뒤에 있는 것이 count(*)의 별칭으로 지정하겠다는 의미로. 

#오늘의 숙제:네이버 메일을 사용하여 앱개발 종합반 신청한 주문문의 결제수단별 주문거수

select payment_method, count() from orders
where email like '%@naver.com' and course_title = '앱개발 종합반'
group by payment_method
order by count(
) desc

결국 
select 변수명,  count (*) from 테이블명 
group by 
--> 엑셀에서의 subtotal... 항목별 개수인것임  
(이거 사실 매우매우 유용한 기능인듯! 연대별 탄소연대 bp값 개수라던지, )

만약 
select 변수명, sum(다른변수 ) from 테이블명 
select랑 변수명만 넣는다면 엑셀에서  주변 변수를 다 숨기기처리하는 것과 같음. 

0개의 댓글