카테고리 없음

SQL개발일지

Jinsol 2022. 1. 22. 16:34

1주차 

select * from [Table]

where [Field] 

between [값] and [값]

 

#필드나 숫자는 따옴표(')나 쌍따옴표(")로 안묶어도됨.

#문자는 따옴표(')나 쌍따옴표(")로  묶어야함.

 

!= 같지 않음(#여집합 개념으로 이해함)

>= 이상

 

ex)

SELECT * from orders
where email like '%naver.com'(# %는 naver.com 앞에 모든 경우의 변수가 올 수 있다는 뜻으로 이해함)
and course_title = '웹개발 종합반'
and payment_method = 'kakaopay'

 

 

<이외 유용한 문법>

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

select * from orders

where payment_method = "kakaopay"

limit 5;

 

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

select distinct(payment_method) from orders;

 

몇 개인지 숫자 세보기: Count

select count(*) from orders

select payment_method from orders;

 

select DISTINCT(payment_method) from orders;

------------------

2주차

 

통계: 최대(max) / 최소(min) / 평균(avg) / 개수(count)

통계 구하기: 기존 방법의 한계

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

깔끔하게 데이터를 정렬해보자: Order by

 

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

ex1) 앱개발 종합반의 결제수단별 주문건수 세어보기

SELECT payment_method, COUNT(*) FROM  orders
where course_title = '웹개발 종합반'
group BY payment_method;

ex2) Gmail 을 사용하는 성씨별 회원수 세어보기

SELECT name, COUNT(*) from users
where email like '%gmail.com'
group BY name;

 

ex3) course_id별 '오늘의 다짐'에 달린 평균 like 개수 구해보기

SELECT course_id, round(avg(likes), 0) from checkins
group by course_id;

깔끔하게 데이터를 정렬해보자: Order by

문자/숫자 둘다 가능하며 기본 asc(오름차순)으로 정렬됨. desc(내림차순)으로 하고 싶으면 아래와 같이 하면 됨.

select * from users

order by created_at desc;(# created_at 필드 값을 내림차순으로 정렬해준다.)

 

 

 

ex4) 네이버 이메일을 사용하여 앱개발 종합반을 신청한 주문의 결제수단별 주문건수 세어보기

select payment_method , count(*) from orders
where email LIKE '%naver.com' and course_title = '앱개발 종합반'
group by payment_method