*사용하는 프로그램: DBeaver
쿼리(Query)문이란?
쿼리는 질의를 의미하죠. 데이터베이스에 명령을 내리는 것을 의미합니다.
※ 코드 형식
-> select <특정 필드 또는 모든 필드(*)> from <테이블>
Where 절은, Select 쿼리문으로 가져올 데이터에 조건을 걸어주는 것을 의미해요
*Where 절과 자주 같이 쓰이는 문법
1) 같지 않음
!=
2) 범위 조건
between
select * from orders
where created_at between "2020-07-13" and "2020-07-15";
3) 포함 조건
in
select * from checkins
where week in (1, 3);
4) 패턴(문자열 규칙) 조건
like
select * from users
where email like '%daum.net';
5) 일부 데이터만 가져오기
limit
select * from orders
where payment_method = "kakaopay"
limit 5;
*소수의 데이터만 뽑아보고 싶을 때
6) 중복 데이터는 제외하고 가져오기
distinct
select distinct(payment_method) from orders;
7) 갯수 세보기
ex)이번에는 orders 테이블에 데이터가 몇 개 들어있는지 궁금해요!
(결제가 몇 건이나 들어왔죠? 두근두근)
select count(*) from orders
8) distinct와 count 같이 써보기
ex) 스파르타 회원 분들의 성(family name)씨가 몇개인지 궁금하다면?
select distinct(name) from users;
SELECT count(distinct(name)) from users;
숙제: naver 이메일을 사용하면서, 웹개발 종합반을 신청했고 결제는 kakaopay로 이뤄진 주문데이터 추출하기
select * from orders
where email like '%naver.com'
and course_title = '웹개발 종합반'
and payment_method = 'kakaopay'
SQL은 그래도 전에 공부해본 적이 있어서 그런지,
원래 단순한 코드들이라 그런건지 쉽게 느껴진다.
그래도 직접 여러 코드를 써보면서 자료를 잘 다룰 수 있게 숙달되야 할 것 같다.