SQL 쿼리 작성

SQL 1주차 수업

kawagoe yura 2022. 7. 27. 08:02

#1 어떤 tables이 있는지 보여줘 = show tables

table이란? 필드별로 데이터가 정리되어있는 표

한 테이블의 내용을 몽땅 가지고 온다 = select * from orders

한 테이블의 특정 필드(들)만 가지고 온다 = select payment_method, email from orders

 

#2 where 문법 연습하기

where 절은, Select 쿼리문으로 가져올 데이터에 조건을 걸어주는 것을 의미한다.

 

orders 테이블에서 결제수단이 카카오페이인 데이터만 가져와줘

= select * from orders 

where payment_method = 'kakaopay'

 

point_users 테이블에서 포인트가 5000 이상인 테이터만 가져와줘

= select * from point_users

where point >= 5000

 

orders 테이블에서 주문한 강의가 앱개발 종합반이면서, 결제수단이 카드인 데이터만 가져와줘

= select * from orders

where course_title = '앱개발 종합반' and payment_method = 'kakaopay'

 

users 테이블에서 성이 황 씨인 유저만 뽑기

= select * from users

where name = '황**'

 

#2_1 where 절과 자주 같이쓰는 문법 배우기

'같지않음' 조건 걸어보기

웹개발 종합반을 제외하고 주문데이터를 가져와줘

= select * from orders

where course_title != '웹개발 종합반'

 

'범위' 조건 걸어보기

7월 13일, 14일의 주문데이터만 가져와줘

= select * from orders

where created_at between '2020-07-13' and '2020-07-14' 

 

'포함' 조건 걸어보기

1, 3주차 사람들의 '오늘의 다짐' 데이터만 가져와줘

= select * from checkins

where week in (1, 3)

 

'패턴'(문자열 규칙) 조건 걸어보기

다음(daum) 이메일을 사용하는 유저만 가져와줘

= select * from users

where email like '%@daum.net'

 

#3 where 문법과 쓰는 다른 문법

일부 데이터만 가져오기 : limit

select * from orders

where payment_method = 'kakaopay'

limit 5

 

중복된 데이터는 제외하고 가져오기 : distinct

select distinct(payment_method) from orders

 

몇 개인지 숫자 세보기 : count

select count(*) from orders

 

스파르타 회원 분들의 성씨가 몇개인지 궁금하다면?

select count( distinct(name) ) from users

 

#4 총 복습 문제 풀어보기

성이 남씨인 유저의 이메일만 추출하기

= select email from users

where name ='남**'

 

Gmail을 사용하는 2020/07/12~2020/07/13에 가입한 유저를 추출하기

= select * from users

where email like '%@gmail.com'

and created_at between '2020-07-12' and '2020-07-14'

 

Gmail을 사용하는 2020/07/12~2020/07/13에 가입한 유저의 수를 세기

= select count (*) from users

where email like '%@gmail.com'

and created_at between '2020-07-12' and '2020-07-14'