[수업 목표]
RDBS에서 쓰는 걸로
그후 쿼리 입력창이 안나오는데 이걸 나오게 하기 위해 위 네비바중 
sql이라고 되어있는 양피지 모양 클릭
select * from student
이게 기본
select * from orders
where payment_method = "CARD" and course_title = "앱개발 종합반";
성이 황씨인 유저만 뽑아보기
select * from users
where name ="황**";
where절과 자주 같이 쓰는 문법 써보기
- 같지 않음: !=
- 범위 between a and b
- 포함 in
- 문자열 규칙: like 사용
%(특정 문자가 포함되기만 하고 그 이전이나 이후에 어떤 문자가 몇 개가 오든지 상관없다는 의미)
(어떤 문자가 오든 상관없다는 의미로 사용한다.
%와의 차이점은 %는 몇 개의 문자가 오든 상관이 없지만 는 단 한 문자에 대해서만 와일드 카드 역할)
이스케이프시 \ 사용(이거 파이썬 인가도 이랬던거 같은데)
select * from orders
where payment_method != 'CARD'; select * from point_users
WHERE `point` BETWEEN 20000 and 30000;
select * from users
WHERE email like "s%com";
select * from users
WHERE email like "s%" and name like "이%";
select email from users
WHERE name like "남%"
3.Gmail을 사용하는 2020/07/12~13에 가입한 유저를 추출하기
select * from users
WHERE email like "%gmail.com" and created_at between "2020-07-12" and "2020-07-14";
4Gmail을 사용하는 2020/07/12~13에 가입한 유저의 수를 세기
select count(*) from users
WHERE email like "%gmail.com" and created_at between "2020-07-12" and "2020-07-14";
숙제
naver 이메일을 사용하면서, 웹개발 종합반을 신청했고 결제는 kakaopay로 이뤄진 주문데이터 추출하기
select * from orders
WHERE email like "%naver.com" and payment_method = 'kakaopay' and course_title = "웹개발 종합반";
[수업 목표]
동일한 범주의 데이터를 묶어서 통계를 내주는 Group by를 이해한다.
출력하는 데이터를 필드의 값으로 정렬하여 출력하는 Order by를 익힌다.
조금 더 복잡한 분석을 위해 자주 사용되는 유용한 문법을 익힌다.
범주의 통계를 내주는 group by
회원이 각 성씨별로 몇명 있는지를 알아보자
select name, count(*) from users
group by name;
이렇게 하면 같은 값끼리 묶여서 count된다
select name, count(*) from users
group by name;
위 쿼리가 실행되는 순서: from → group by → select
1. from users: users 테이블 데이터 전체를 불러오기
2. group by name: users 테이블 데이터에서 같은 name을 갖는 데이터를 합치기
3. select name, count(*): name에 따라 합쳐진 데이터가 각각 몇 개 합친건지 count
select COUNT(comment) from checkins
group by week;
- 동일한 범주에서 최솟값 구하기
count부분을 min으로
``` sql
select min(likes) from checkins
group by week;
select avg(likes) from checkins
group by week;
select max(likes) from checkins
group by week;
select sum(likes) from checkins
group by week;
select name, count(*) from users
group by name
order by count(*);
select * from users
order by email;
이렇게 하면 문자열(알파벳, 한글등) 순으로 정렬
시간을 기준으로 정렬
최근 시간을 보고 싶을때는 뒤에 desc
select * from users
order by created_at desc;
2)group by 연습하기
앱개발종합반의 결제수단별 주문건수 세어보기
select payment_method ,COUNT(payment_method) from orders
where course_title = "앱개발 종합반"
group by payment_method;
Gmail 을 사용하는 성씨별 회원수 세어보기
SELECT name, count(name) from users
where email like "%gmail.com"
group by name;
쓸때 먼저 select where 쓰고 group by order by
course_id별 '오늘의 다짐'에 달린 평균 like 개수 구해보기
SELECT course_id ,avg(likes) FROM checkins
group by course_id ;
select * from orders o
where o.course_title = '앱개발 종합반'
이렇게 테이블 뒤에 as없이 바로 별명 붙여서도 사용가능하고(이건 다음에 join시 더 직관적이고 쉽게 입력 위해 사용하는 경우가 많았던거 같다)
select payment_method, count(*) as cnt from orders o
where o.course_title = '앱개발 종합반'
group by payment_method
이렇게 출력될 필드에 붙이는것도 가능하다
2주차 숙제
네이버 이메일을 사용하여 앱개발 종합반을 신청한 주문의 결제수단별 주문건수 세어보기
SELECT payment_method, COUNT(payment_method) from orders o
where email LIKE "%naver.com"
and course_title = "앱개발 종합반"
group by payment_method;
오랜만에 관계형 쓰니까 그때 수업 받을때 느낌 난다