SQL

sql 기초 + group by / order by / where 쀑볡

GABOJOK 2023. 9. 23. 23:00

limit 숫자

πŸ‘‰  ν…Œμ΄λΈ”μ—μ„œ 데이터 '숫자' 개수 만큼만 μž˜λΌμ„œ λ³΄μ—¬μ€˜.

select * from checkins
limit 5

 

 

 

max(ν•„λ“œλͺ…)   /  min(ν•„λ“œλͺ…)

πŸ‘‰  μ΅œλŒ“κ°’ κ΅¬ν•˜κΈ° /  μ΅œμ†Ÿκ°’ κ΅¬ν•˜κΈ°.

select week, min(likes) from checkins
group by week

 

 

avg(ν•„λ“œλͺ…)

πŸ‘‰  ν‰κ·  κ΅¬ν•˜κΈ°.

select week,avg(likes) from checkins
group by week

 

 

round( , 숫자)

πŸ‘‰  μ†Œμˆ«μ  "숫자" 자리 κΉŒμ§€λ§Œ λ‚˜μ˜€κ²Œ 반올림 ν•΄μ€˜  

예 ) round κ΄„ν˜Έ 끝에 적힌 μˆ«μžκ°€ 2라면, μ†Œμˆ«μ  2μ§Έ 자리 κΉŒμ§€λ§Œ λ‚˜μ˜€κ²Œ ν•΄μ€˜.

select week, round(avg(likes),3) from checkins
group by week

 

 

count(ν•„λ“œλͺ…)

πŸ‘‰  ν•΄λ‹Ή ν•„λ“œμ˜ 개수 κ΅¬ν•˜κΈ°. *둜 ν•˜λ©΄ 전체 개수 κ΅¬ν•˜λŠ” 것. 

πŸ‘‰  countλŠ” NULL을 μ„Έμ§€ μ•ŠλŠ”λ‹€

 
select week, count(likes) from checkins
group by week

 

 

sum(ν•„λ“œλͺ…) 

πŸ‘‰  ν•„λ“œ μ•ˆμ— 데이터듀 λ‹€ λ”ν•΄μ€˜. 합계 κ΅¬ν•˜κΈ°.

select week, sum(likes) from checkins
group by week

 

 

group by ν•„λ“œμ΄λ¦„

πŸ‘‰  동일 ν•„λ“œ λ‚΄μ˜ 데이터λ₯Ό ν•˜λ‚˜λ‘œ 묢을 수 있음.

name ν•„λ“œμ˜ 데이터가 이** κΉ€** μ‹μœΌλ‘œ μ €μž₯λ˜μ–΄ μžˆλ‹€λ©΄, group byλ₯Ό 톡해 κ°™μ€ name을 κ°–λŠ” 데이터λ₯Ό ν•©μΉ¨.

select * from users u 
group by name

 

 

 

order by ν•„λ“œμ΄λ¦„ 

πŸ‘‰  데이터 μ •λ ¬ν•˜κ³ μž ν• λ•Œ 씀. λ””ν΄νŠΈλŠ” μ˜€λ¦„μ°¨μˆœ. asc 

πŸ‘‰  λ‚΄λ¦Όμ°¨μˆœ  desc

πŸ‘‰  숫자, λ¬Έμžμ—΄, μ‹œκ°„λ„ 정렬이 κ°€λŠ₯함

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

 

정렬은 μ–Έμ œλ‚˜ λ§ˆμ§€λ§‰μ— ν•˜μž. μ–΄μ°¨ν”Ό 맨 λ§ˆμ§€λ§‰μ— 싀행됨.

 

 

 

order by 쑰건의 쀑볡   /  group by 쑰건의 쀑볡

πŸ‘‰   μ—¬λŸ¬κ°œ μ“°κ³  μ‹ΆμœΌλ©΄ , μ΄μš©ν•˜λ©΄ λœλ‹€.

select * from orders o 
group by payment_method , course_id 
order by course_id , payment_method desc

 

 

 

where 쑰건

πŸ‘‰  μ›ν•˜λŠ” 쑰건에 ν•΄λ‹Ήν•˜λŠ” λ°μ΄ν„°λ§Œ κ°€μ Έμ˜¬ 수 μžˆλŠ” μ•„μ£Ό λ˜‘λ˜‘ν•œ 칭ꡬ.

select payment_method, count(*) from orders
where course_title = "μ›Ήκ°œλ°œ μ’…ν•©λ°˜"
group by payment_method;
 
 

where (ν•„λ“œλͺ…) like   '% gmail %'

πŸ‘‰  % λŠ” μ–΄λ–€κ²Œ μ˜€λ“  상관 μ—†λ‹€λŠ” 의미.
πŸ‘‰  ν•„λ“œλͺ…에 ν•΄λ‹Ήν•˜λŠ” 데이터듀 쀑  gmail 이 λ“€μ–΄κ°„ λ°μ΄ν„°λ§Œ 가져와 λ‹¬λΌλŠ” λ§μž„.
select name, count(*) from users
where email like '%gmail.com'
group by name;

 

 

where (ν•„λ“œλͺ…)  between  'μ–Έμ œλΆ€ν„°'  and  '이거 μ „ κΉŒμ§€'

πŸ‘‰  created_at λ‚ μ§œκ°€ 2020-08-01 λΆ€ν„° ~ 2020-10-10 μ „κΉŒμ§€ ν•΄λ‹Ήν•˜λŠ” 자료만 가져와 λ‹¬λΌλŠ” 말이닀.

select * from orders o 
where created_at between '2020-08-01' and '2020-10-10'

 

 

 

where  쑰건  or 쑰건 

πŸ‘‰  λ‘˜μ€‘ ν•˜λ‚˜λΌλ„ 쑰건에 ν•΄λ‹Ήν•œλ‹€λ©΄ λ³΄μ—¬λ‹¬λΌλŠ” μ˜λ―Έμ΄λ‹€. 

select * from orders o 
where (created_at between '2020-08-01' and '2020-10-10') or 
	  (created_at between '2020-07-01' and '2020-07-15')

 

 

where  ν•„λ“œ  in  (쑰건)

select * from users
where name in ('μ‹ **', '이**')

 

πŸ‘‰  ν•˜λ‚˜λΌλ„ 쑰건에 ν•΄λ‹Ήν•œλ‹€λ©΄ λ³΄μ—¬λ‹¬λΌλŠ” μ˜λ―Έμ΄λ‹€. 

πŸ‘‰  or 와 같은 μ—­ν•  함.

πŸ‘‰  κ΄„ν˜Έ μ•ˆμ— 쑰건듀을 콀마 (,) 둜 κ΅¬λΆ„ν•΄μ„œ λ„£μ–΄μ€€λ‹€. 

πŸ‘‰  in 의 κ΄„ν˜Έ μ•ˆμ— λ‹€λ₯Έ select 문을 넣을 수 μžˆλ‹€. 

 

select * from users u 
where u.user_id in (select o.user_id
	from orders o 
	where o.payment_method = 'kakaopay');

 

 

 

 

 

where 쑰건의 쀑볡

πŸ‘‰   μ—¬λŸ¬κ°œ μ“°κ³  μ‹ΆμœΌλ©΄ and μ΄μš©ν•˜λ©΄ λœλ‹€.

select payment_method, count(*) from orders 
where course_title = "μ•±κ°œλ°œ μ’…ν•©λ°˜" 
and email like '%naver.com'
group by payment_method

 

 

 

 

alias κΈ°λŠ₯ 

as λ³„μΉ­μ§€μ–΄μ£Όμ„Έμš”         

πŸ‘‰  sql의 쿼리문이 λ³΅μž‘ν•΄ μ‘Œμ„λ•Œ ν—·κ°ˆλ¦¬μ§€ μ•Šλ„λ‘ alias λ₯Ό 톡해 이름 μ§€μ–΄λŠ” κΈ°λŠ₯.

πŸ‘‰  ν…Œμ΄λΈ” λͺ…, ν•„λ“œλͺ… λͺ¨λ‘ 뢙일 수 μžˆλ‹€.

select * from orders as o
where o.course_title = "μ•±κ°œλ°œ μ’…ν•©λ°˜";
select name, count(*) as freq from users
group by name