๐Ÿ”ฅ TIL - Day 38 - 1

Kim Dae Hyunยท2021๋…„ 10์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
45/93

๐Ÿ“Œgroup by (~ ๋ณ„๋กœ ์กฐํšŒํ•œ๋‹ค)

ํŠน์ • ํ•„๋“œ๋ณ„๋กœ ๊ฐœ์ˆ˜๋ฅผ ์กฐํšŒํ•œ๋‹ค.

select ํ•„๋“œ, count(*) from ํ…Œ์ด๋ธ”
group by ํ•„๋“œ

ex) group by + where
select ํ•„๋“œ, count(*) from ํ…Œ์ด๋ธ”
where ์กฐ๊ฑดํ•„๋“œ = ์กฐ๊ฑด
group by ํ•„๋“œ

์—ฌ๋Ÿฌ ์ง‘๊ณ„์ฟผ๋ฆฌ์™€ ํ•ฉ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.

  • ~๋ณ„๋กœ ๋ฏ‚์–ด์„œ ๊ฐœ์ˆ˜๋ฅผ ์กฐํšŒํ•œ๋‹ค. count
  • ~๋ณ„๋กœ ๋ฌถ์–ด์„œ ํŠน์ • ํ•„๋“œ์˜ ํ•ฉ๊ณ„๋ฅผ ์กฐํšŒํ•œ๋‹ค. sum
  • ~๋ณ„๋กœ ๋ฌถ์–ด์„œ ํŠน์ • ํ•„๋“œ์˜ ์ตœ์†Œ๊ฐ’์„ ์กฐํšŒํ•œ๋‹ค. min
  • ~๋ณ„๋กœ ๋ฌถ์–ด์„œ ํŠน์ • ํ•„๋“œ์˜ ์ตœ๋Œ€๊ฐ’์„ ์กฐํšŒํ•œ๋‹ค. max
  • ~๋ณ„๋กœ ๋ฌถ์–ด์„œ ํŠน์ • ํ•„๋“œ์˜ ํ‰๊ท ๊ฐ’์„ ์กฐํšŒํ•œ๋‹ค. avg


๐Ÿ“Œ order by

  • ์ •๋ ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉ
  • ์ฟผ๋ฆฌ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•œ๋‹ค.
ex) ํ•„๋“œ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ  (๊ธฐ๋ณธ๊ฐ’= ์˜ค๋ฆ„์ฐจ์ˆœ asc)
select * from ํ…Œ์ด๋ธ”
order by ํ•„๋“œ

ex) ํ•„๋“œ๋ณ„๋กœ ๋ฌถ์—ˆ์„ ๋•Œ ๊ฐœ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
select ํ•„๋“œ, count(*) from ํ…Œ์ด๋ธ”
group by ํ•„๋“œ
order by count(*) desc


๐Ÿ“Œ Join

left join

  • A์™€ B ๋‘ ํ…Œ์ด๋ธ”์„ joinํ•  ๋•Œ A์™€ B์˜ ๊ต์ง‘ํ•ฉ ๋ถ€๋ถ„๊ณผ join์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ์™ผ์ชฝ ํ…Œ์ด๋ธ” A์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” join ๋ฐฉ์‹์ด๋‹ค. (์™ผ์ชฝํ…Œ์ด๋ธ”(A) + ๊ต์ง‘ํ•ฉ)
  • from ์ ˆ์— ์˜ค๋Š” ํ…Œ์ด๋ธ”์ด ๊ธฐ์ค€์ด ๋˜๋Š” ํ…Œ์ด๋ธ” (์™ผ์ชฝ ํ…Œ์ด๋ธ”)์ด ๋œ๋‹ค.
  • ๋งŒ์•ฝ ๊ต์ง‘ํ•ฉ์ด ์—†๋Š” A ํ…Œ์ด๋ธ”์˜ ๊ฒฝ์šฐ B์— ํ•ด๋‹นํ•˜๋Š” ์นผ๋Ÿผ๋“ค์„ null๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.
select * from A_table a
left join B_table b
on a.user_id = b.user_id

inner join (default)

  • ๊ต ์ง‘ ํ•ฉ
select * from A_table a
[inner] join B_table b 
on a.user_id = b.user_id

ex)
ordersํ…Œ์ด๋ธ”๊ณผ usersํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด์„œ ๋„ค์ด๋ฒ„ ์ด๋ฉ”์ผ์„ ์‚ฌ์šฉํ•˜๋Š” ์œ ์ €์˜ ์„ฑ์”จ๋ณ„ ์ฃผ๋ฌธ๊ฑด์ˆ˜ ์กฐํšŒ (user_id๋ฅผ fk๋กœ ๊ฐ€์ง„๋‹ค.)

select u.name, count(*) as count from orders o 
join users u 
on o.user_id = u.user_id
where u.email like '%naver.com'
group by u.name;

ex)
๊ฒฐ์ œ์ˆ˜๋‹จ ๋ณ„ user์˜ point ํ‰๊ท ๊ฐ’ ์กฐํšŒํ•˜๊ธฐ (์†Œ์ˆ˜์  3๋ฒˆ์งธ๊นŒ์ง€ ํ‘œ์‹œ)
(orders์™€ point_users ํ…Œ์ด๋ธ” ์ด์šฉ fk: user_id)

select o.payment_method, round(avg(pu.point),3) from orders o
join point_users pu 
on o.user_id = pu.user_id
group by o.payment_method

ex)
course์˜ ์ด๋ฆ„ ๋ณ„๋กœ week ๋ณ„ ์ฒดํฌ์ธ ์ˆ˜ ์กฐํšŒ
courses ํ…Œ์ด๋ธ”๊ณผ checkins ํ…Œ์ด๋ธ” ์ด์šฉ (fk: course_id)

[ 2๊ฐœ ํ•„๋“œ๋กœ ๊ทธ๋ฃนํ™” ๋ฐ ์ •๋ ฌ ]

select c1.title, c2.week, count(*) as cnt 
from courses c1
join checkins c2
on c1.course_id = c2.course_id
group by c1.title, c2.week
order by c1.title, c2.week

์œ„ ์ฟผ๋ฆฌ์—์„œ 8์›” 1์ผ ์ดํ›„ ์ฃผ๋ฌธํ•œ ๊ณ ๊ฐ์˜ ๊ฐ•์˜๋งŒ ์กฐํšŒ ์ถ”๊ฐ€
์ด 3๊ฐœ ํ…Œ์ด๋ธ”์„ join (course, checkins, orders)

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


๐Ÿ“Œ in ์ฟผ๋ฆฌ

  • where์ ˆ์˜ ํ•„๋“œ๊ฐ€ in ์— ์ž‘์„ฑํ•œ ๊ฐ’์ธ ํ–‰๋งŒ ์กฐํšŒํ•œ๋‹ค.
  • ๋น„๊ต์  ์ข‹์€ ์„ฑ๋Šฅ์„ ๊ฐ–๋Š” ํŽธ (or๋กœ ์กฐ๊ฑด์„ ์—ด๊ฑฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ธ๋‹ค.)
select * from ํ…Œ์ด๋ธ”
where ํ•„๋“œ in (๊ฐ’1, ๊ฐ’2, ๊ฐ’3)


๐Ÿ“Œ Subquery

  • select์ ˆ, from์ ˆ, where์ ˆ ๋“ฑ์— ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
  • join, in ๋“ฑ์˜ ๋Œ€์ƒ์œผ๋กœ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

select์ ˆ Subquery

  • ๋งค ํ–‰๋งˆ๋‹ค select ์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
select c.checkin_id,
       c.user_id,
       c.likes,
       (
 	      select avg(likes) from checkins
 	      where user_id = c.user_id
       	)
from checkins c     

Subquery๋ฅผ join์˜ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

  • Subquery์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งˆ์น˜ ํ•œ ๊ฐœ ํ…Œ์ด๋ธ”์ธ ๊ฒƒ์ฒ˜๋Ÿผ join์˜ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
[ user_id๋ณ„ ์ข‹์•„์š”๋ฅผ ๋ฐ›์€ ํ‰๊ท ์„ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ ]
[ ์ด ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ join์˜ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ]
select user_id, avg(likes) as avg_likes from checkins
group by user_id

select pu.user_id, pu.point, u.avg_likes 
from point_users pu
join (
	select user_id, avg(likes) as avg_likes from checkins
	group by user_id
) as u
on pu.user_id = u.user_id

where์ ˆ์˜ ์กฐ๊ฑด์œผ๋กœ Subquery ์‚ฌ์šฉํ•˜๊ธฐ

[ ์ „์ฒด point์˜ ํ‰๊ท ๋ณด๋‹ค ํฐ point๋ฅผ ๊ฐ€์ง„ user๋งŒ ์กฐํšŒ ]
select * from point_users pu
where pu.point > (
	select avg(point) from point_users
)


[ ์œ„ ์กฐ๊ฑด์—์„œ ์ด์”จ ์„ฑ์„ ๊ฐ€์ง„ ์œ ์ €๋ผ๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ ]
[ join์œผ๋กœ ํ•ด๊ฒฐ ]
select * from point_users pu
where pu.point > (
	select avg(point) from point_users pu
	join users u 
	on pu.user_id = u.user_id
	where u.name like '์ด%'
) 

[ ์œ„ ๋ฌธ์ œ๋ฅผ in์ฟผ๋ฆฌ๋ฅผ ์ด์šฉ ]
[ ์„œ๋ธŒ์ฟผ๋ฆฌ ์•ˆ์— ๋˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋Š” ํ˜•ํƒœ ]
select * from point_users pu
where pu.point > (
	select avg(point) from point_users
	where user_id in (
		select user_id from users where name like '์ด%'
	)
) 

in ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•œ Subquery

  • in ์•ˆ์ชฝ์˜ select ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ in ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
select * from ํ…Œ์ด๋ธ”1
where ํ•„๋“œ in (
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”2
	where ์กฐ๊ฑดํ•„๋“œ = ์กฐ๊ฑด
)

with์ ˆ ์‚ฌ์šฉ

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
  • ์•ž์„œ ์‚ฌ์šฉํ•ด๋ณธ from ์ ˆ์—์„œ Subquery๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ Subquery๋ฅผ join์˜ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ with๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
select *
from (
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”1
	...
) a
join 
(
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”2
	...
) b on a.id = b.id
join
(
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”3
	...
) c on a.id = c.id

์œ„์™€ ๊ฐ™์ด Subquery์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งˆ์น˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ with๋ฅผ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด with๋ฅผ ์ด์šฉํ•ด์„œ ์ž„์‹œ๋กœ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๋Š๋‚Œ?

with tableA as (
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”1
	...
), tableB as (
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”2
	...
), tableC as (
	select ํ•„๋“œ from ํ…Œ์ด๋ธ”3
	...
)

select * 
from tableA a
join tableB b on a.id = b.id
join tableC c on a.id = c.id

[ ๊น”____๋” ]


์˜ค๋Š˜์˜ ๋ฝ€์ธํŠธ๋Š” group by, join, subquery

์˜ค๋Š˜์˜ SQL์€ ์—ฌ๊ธฐ๊นŒ์ง€

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€