SQL 3주

Hwang Siyeon·2022년 11월 22일
0

SQL

목록 보기
3/4

<엑셀보다 쉬운 SQL> - 3주차 개발일지

벌써 3주차다. 어느정도 익숙해지려고 한다.
이 과정을 다 들으면 자격증 준비도 해보려 한다.
문득 내가 이걸 해서 취업이 잘 될까? 생각이 들곤 하지만, 뭐든 배워놓고 후회한 적은 없기 때문에 그냥 묵묵히 하련다.

3주차 개발일지

Join을 위주로 배웠고, Union을 간단히 배웠다.

JOIN
: 두 테이블의 공통된 정보(key값)을 기준으로 테이블을 연결하는 것
left join 과 inner join 이 있다

left join
: 중복되는 값이 아니라면 NULL 처리 --> 기준이 되는 테이블 all

inner join
: NULL 없이 연결 --> 교집합

어려웠던 예제를 기록해보려 한다.

웹개발, 앱개발 종합반의 week별 체크인수 세어보기
(8월 1일 이후 구매 고객만)

select c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at >= '2020-08-01'
group by c1.title, c2.week
order by c1.title, c2.week

7월 10일 ~ 7월 19일에 가입한 고객 중, 포인트를 가진 고객의 숫자, 그리고 전체 숫자, 그리고 비율
*소숫점 둘째자리까지 표시

select count(point_user_id), 
count(*),
round(count(point_user_id)/count(*),2)
from users u
left join point_users pu on u.user_id = pu.user_id
where u.created_at between '2020-07-10' and '2020-07-20'

union all
: Select를 두번 하지 않고, 한번에 모아서 보고싶은 경우

(
	select '7월' as month, c.title, c2.week, count(*) as cnt 
    from checkins c2
	inner join courses c on c2.course_id = c.course_id
	inner join orders o on o.user_id = c2.user_id
	where o.created_at < '2020-08-01'
	group by c2.course_id, c2.week
  order by c2.course_id, c2.week
)
union all
(
	select '8월' as month, c.title, c2.week, count(*) as cnt 
    from checkins c2
	inner join courses c on c2.course_id = c.course_id
	inner join orders o on o.user_id = c2.user_id
	where o.created_at > '2020-08-01'
	group by c2.course_id, c2.week
  order by c2.course_id, c2.week
)

점점 어려워지는 만큼 해내면 성취감이 크다! :)

profile
learn more!

0개의 댓글