SQL 2일차

숨오·2022년 5월 5일

Group by
Order by

users에서 name으로 묶임 + name으로 묶인 것 당 count해서 보여줄 것
그룹별로 묶어줄 것 + 각 성씨별로 갯수를 세주는 것
Select name, count(*) from users
group by name

최소값 min
like 의 최소값

max

avg

sum

반올림 round

select 범주가 담긴 필드명,

order by 정렬
기본적으로 오름차순 표기 (asc)
내림차순 표기(desc)

users에서 name 으로 묶었다

select name,count() from users
group by name
order by count(
)

order by

select payment_method, count() from orders
where course_title = '웹개발 종합반'
group by payment_method
order by count(
)

select payment_method , count(*) from orders
where course_title = '앱개발 종합반'
group by payment_method

select name,count(*) from users
where email like '%gmail.com'
group by name

select * from orders o
where o.course_title = '앱개발 종합반'

2주차 숙제
o.payment_method ,count(*) from orders o
where o.email like '%naver.com'
and o.course_title = '앱개발 종합반'
group by o.payment_method


alias 별칭


join: 테이블 매칭시킬 때 사용하는 것이 join(=vlookup)
Left join: A + (A를 기준으로 B의 값까지) = 합집합
-> users 테이블에 point_users 테이블을 붙이는 것 / on 기준
select * from users u
left join point_users pu on u.user_id = pu.user_id

Inner join: A와 B의 교집합
select * from users u
inner join point_users pu on u.user_id = pu.user_id

1) 테이블을 왜 나눠놓는 것인가?
테이블은 한 목적에 맞춰 저장해두는 것이 좋음

2) 무엇으로 테이블 여러개를 잇는가?
기준이 필요함 (이 때 사용하는 것이 join)

[Quiz]
1. orders 테이블에 users 테이블 연결해보기
select * from orders o
inner join users u
on o.user_id =u.user_id
-> left join은 A에 B를 붙여라, 없으면 null로 보여줘야하기때문에 누구한테 누구를 붙여야하는 지가 매우 중요함

  1. checkins 테이블에 users 테이블 연결해보기
    select * from checkins c
    inner join users u
    on c.user_id = u.user_id

  2. enrolleds 테이블에 courses 테이블 연결해보기
    select * from enrolleds e
    inner join courses c
    on e.course_id = c.course_id

profile
자신감만땅

0개의 댓글