[SQL 입문] Week 1
1. Intro.
반복적으로 써보면서 감과 원리는 익히기~!
SQL이 언제 필요한지?
-> 방대한 양의 데이터 저장 후, 원하는 자료를 신속하게 가져올 수 있음
-> Create 데이터 생성 / Read 저장된 데이터 읽어 오기 / Update 저장된 데이터 변경 / Delete 저장된 데이터 삭제
-> Read만 배우는 강의 임
-> 데이터베이스와 소통하는 언어가 SQL
2. D비버 설치
Download | DBeaver Community
Download Tested and verified for MS Windows, Linux and Mac OS X. Install: Windows installer – run installer executable. It will automatically upgrade version (if needed). MacOS DMG – just run it and drag-n-drop DBeaver into Applications. Debian package
dbeaver.io
3. 기본 개념
테이블 : 데이터가 담겨 있는 표
필드 : "order_no" "email" 등
4.
(1) 테이블 보여줘
show tables
(2) 모든필드 선택
select * from orders
(* == 모든 필드 이라는 의미)
(3) 특정 필드 선택
select order_no, created_at, user_id, email from orders
5. Select, Where 절
(1) 카카오페이로 결재한 고객 선택
select * from orders
where payment_method = 'kakaopay'
kakopay 앞뒤에 ' ' 꼭 넣어야 문자열로 인식
(2) 포인트가 5천점 이상
select * from point_users
where point >= 5000
(3) 웹개발 반이면서 카드로 결재한 사람
select * from orders
where course_title = '앱개발 종합반' and payment_method = 'CARD'
(4) 포인트가 2만점 보다 많은 유저
select * from point_users
where point > 20000
(5) 성이 황씨인 사람
select * from users
where name = '황**'
(6) 웹개발 종합반 이면서 결제수단이 카드
select * from orders
where course_title = '웹개발 종합반' and payment_method = 'CARD'
<팁>
전체 테이블 보기 - show tables
원하는 테이블을 보고 필드명 확인 - select * from courses 필드명 알고 where절 쓰면 됨
<다른 절 배우기>
!= 같지 않음 조건
= 같다
between ... and 특정 범위 조건 (e.g. 7월 13일, 14일 주문데이터만 보기 - 즉, 15일 00시 00분 전까지의 데이터)
select * from orders
where created_at between '2020-07-13' and '2020-07-15'
in ( , ) 포함 조건
select * from checkins
where week in (1,3)
like '%...' 패턴 (문자열 규칙) 조건
(daum 이메일을 사용하는 유저만 보고 싶다)
select * from users
where email like '%@daum.net'
% 는 앞에 뭐가 있어도 상관 없다는 의미
a%t a로 시작해서 t로 끝난다는 의미
퀴즈 :
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%com' and name = '이**'
<이 외에 유용한 문법>
limit 숫자 - 그 숫자 만큼의 데이터만 보고 싶다는 의미. 데이터가 너무 많을 때 어떻게 생겼는지 확인할 때
select * from orders
where payment_method = 'kakaopay'
limit 5
distinct ( ) - 중복제거하고 보기 - 어떤 답이 있었는지 보고 싶을 때
select distinct(payment_method) from orders
count ( ) - 갯수 세는 것
select count(*) from orders
where payment_method = 'kakaopay'
count와 distinct 를 동시에 사용
select count(distinct(name)) from users
-> 아, 54개의 성씨가 있구나~ 알 수 있음.
1. 퀴즈풀기~~
성이 남씨인 유저의 이메일만 추출하기
select email from users
where name = '남**'
Gmail을 사용하는 2020/07/12~13에 가입한 유저를 추출
select * from users
where email like '%@gmail.com'
and created_at between '2020-07-12' and '2020-07-14
Gmail을 사용하는 2020/07/12~13에 가입한 유저의 수를 세기
select count(*) from users
where email like '%@gmail.com'
and created_at between '2020-07-12' and '2020-07-14
2. 숙제 하기~~
naver 메일을 사용하면서, 웹개발 종합반을 신청했고 결제는 kakaopay로 이뤄진 주문데이터 추출하기
select * from orders
where email like '%@naver.com'
and course_title = '웹개발 종합반'
and payment_method = 'kakaopay'
-끝-