sql 왕초보 버전으로 수강해보니 이거 매우 엑셀스럽다.
join on
엑셀의 vlookup 과 유사한 기능으로
두 시트에서 동일한 값을 기준으로 서로 매칭시켜준다.
select * from users u
left join point_users p
on u.user_id = p.user_id
users 시트와 point_users 시트에서
user_id 값으로 매칭
단, 왼쪽 데이터 기준으로 정렬됨.
select * from users u
inner join point_users p
on u.user_id = p.user_id
위의 left join과 결과값은 유사하지만
null 값이 없이 데이터가 꽉 찬 것만 연결.
select u.name, count(u.name) as count_name from orders o
inner join users u
on o.user_id = u.user_id
where u.email like '%naver.com'
group by u.name
from → join → where → group by → select
from orders o: orders 테이블 데이터 전체를 가져오고 o라는 별칭을 붙입니다.
inner join users u on o.user_id = u.user_id : users 테이블을 orders 테이블에 붙이는데, orders 테이블의 user_id와 동일한 user_id를 갖는 users 테이블 데이터를 붙입니다. (*users 테이블에 u라는 별칭을 붙입니다)
where u.email like '%naver.com': users 테이블 email 필드값이 naver.com으로 끝나는 값만 가져옵니다.
group by u.name: users 테이블의 name값이 같은 값들을 뭉쳐줍니다.
select u.name, count(u.name) as count_name : users 테이블의 name필드와 name 필드를 기준으로 뭉쳐진 갯수를 세어서 출력해줍니다.
select o.payment_method, round(avg(pu.point),2) from point_users pu
inner join orders o on pu.user_id = o.user_id
group by o.payment_method
select o.payment_method, round(AVG(p.point)) from point_users p
inner join orders o
on p.user_id = o.user_id
group by o.payment_method
select name, count(*) from enrolleds e
inner join users u
on e.user_id = u.user_id
where is_registered = 0
group by name
order by count(*)
select name, count(*) as cnt_name from enrolleds e
inner join users u
on e.user_id = u.user_id
where is_registered = 0
group by name
order by cnt_name desc
select c.course_id, title, count(*) from courses c
join enrolleds e on c.course_id = e.course_id
where is_registered = 0
group by c.course_id
select c.course_id, c.title, count(*) as cnt_notstart from courses c
inner join enrolleds e
on c.course_id = e.course_id
where is_registered = 0
group by c.course_id
그리고..., 퀴즈에서 제공한 이미지를 잘 관찰해야겠다.
또 필드명을 안바꿨네....
select c1.title, week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
group by c1. title, c2.week
order by c1. title
select c1.title, c2.week, count(*) as cnt from checkins c2
inner join courses c1 on c2.course_id = c1.course_id
group by c1.title c2.week
order by c1.title, c2.week
select c1.title, 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
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
select count(pu.point) as pnt_user_cnt,
count(*) as tot_user_cnt,
round(count(point_user_id)/count(*),2) as ratio
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'
select count(point_user_id) as pnt_user_cnt,
count(*) as tot_user_cnt,
round(count(point_user_id)/count(*),2) as ratio
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'