IT 강의

SQL 강의 핵심요약 #3. group by/order by/as

척척박4 2021. 5. 5. 22:10
728x90
반응형
  • group by : 동일한 값을 갖는 필드를 묶는 기능 (리스트업/카운트/통계)
    아래 데이터에서 from -> group by -> select 순으로 구해짐실행되는 순서 꼭 알아둘 것

select name, count(*) from users group by name; select count(*) from users where name = "남**";

o 주차별 학습목표 개수 구하기

select week, count(*) from checkins group by week;

  • 최대/최소/평균/합계 구하기

select week, max(likes) from checkins group by week; select week, min(likes) from checkins group by week; select week, avg(likes) from checkins group by week; select week, sum(likes) from checkinsgroup by week;

  • order by : ㄱ->ㅎ 순 정렬

SELECT payment_method, count(*) from orders where course_title = "전과목 종합반" group by payment_method order by count(*) desc; //desc = 내림차순

  • as : 복잡한 쿼리문일 때 유용

select * from orders as o where o.course_title ="전과목 종합반" select name as 이름, count(*) as 인원 from users group by name;

728x90
반응형