SQL : group by와 order by

Stian·2023년 6월 22일

통계를 활용해서 데이터 분석하기:Group by, Order BY

##동일한 범주의 데이터를 묶어주는 Group BY

  • group by

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

    #name 전체를 세봐라 -> 그 다음에 name별로 세분화하라는 그런 .. 쿼리가 실행되는 순서는 다음과 같다 먼저 from users해서 usersㄹ 테이브를 가지고 오고, 다음ㅇ로 group by name을 해서 name 안에서 분류된다. 분류된 것들을 count(*)해서 항목별로 개수를 구해준다.

select * from users
where name= "신**" #검토

##Group by의 기능

select * from checkins
where week = 3

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

#계속 오류나다가 바로 윗줄이랑 두개를 같이 블록잡고 마우스 오른쪽버튼으로 실행하니까 이제는 오류 안나오네 .. group by쓰니까 종류별로 나오게 됨.

select week, min(likes) from checkins
group by week

select week, max(likes) from checkins
group by week

select week, avg(likes) from checkins

#week 별 like 평균수
group by week

select week, round(avg(likes),2) from checkins >> #like의 평균을 소수 둘째자리까지.
group by week

select week, sum(likes) from checkins
group by week

#like다 더했을 때 ..저번주에 배웠던 count는 행의 개수를 센다는 느낌이라면 sum은 안에 있는 인덱스를 계산하는 거라는거지~

select * from checkins
where week=2

0개의 댓글