SQL

SQL Subquery / with table1 as / distinct

GABOJOK 2023. 9. 24. 11:55

Subquery

๐Ÿ‘‰   ์ฟผ๋ฆฌ๋ฌธ ์•ˆ์— ๋˜๋‹ค๋ฅธ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ.

๐Ÿ‘‰   where์ ˆ, from ์ ˆ, select ์ ˆ ์—์„œ ์ด์šฉ.

 

 

 

where ์ ˆ์—์„œ ์‚ฌ์šฉํ•œ Subquery   in

๐Ÿ‘‰   Subquery์˜ ๊ฒฐ๊ณผ๋ฅผ ์กฐ๊ฑด์— ํ™œ์šฉ

๐Ÿ‘‰   where ํ•„๋“œ๋ช… in (subquery)

๐Ÿ‘‰   where  ํ•„๋“œ๋ช… ์ด (์„œ๋ธŒ์ฟผ๋ฆฌ) ์— ํ•ด๋‹นํ•  ๋•Œ~~ ์ด๋Ÿฐ๋ง. = ๊ธฐํ˜ธ ์•„๋‹ˆ๊ณ  in ์จ์•ผํ•จ! 

select user_id, name, email from users
where user_id in (
     select user_id from orders
     where payment_method = 'kakaopay'
)

 

โ—๏ธ   Query         ๐Ÿ‘‰  users ํ…Œ์ด๋ธ” ์—์„œ, user_id์™€, name์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”๋ฐ,

โ—๏ธ   Subquery   ๐Ÿ‘‰  orders ํ…Œ์ด๋ธ”์—์„œ payment_method๊ฐ€ kakaopay์ธ  user_id  

โ—๏ธ   where์ ˆ      ๐Ÿ‘‰  ์— ํ•ด๋‹นํ•˜๋Š” user_id๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์• ๋“ค๋งŒ ๊ฐ€์ ธ์™€์ค˜.

 

 

 

 

select ๋ฌธ ์—์„œ ์‚ฌ์šฉํ•œ Subquery

๐Ÿ‘‰   ๊ธฐ์กด ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€ ํ•˜๊ณ  ์‹ถ์€ ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ.

๐Ÿ‘‰   select  ํ•„๋“œ๋ช…,  ํ•„๋“œ๋ช…, (subquery) as ๋ณ„์นญ 

select c.checkin_id, 
       c.user_id, 
       c.likes, 
       (
        select avg(likes) from checkins c2
        where c2.user_id = c.user_id
       ) as avg_like_user
  from checkins c;

 

โ—๏ธ   Query         ๐Ÿ‘‰  checkins c ํ…Œ์ด๋ธ” ์—์„œ, c.checkin_id, c.user_id, c.likes ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”๋ฐ, ์ถ”๊ฐ€๋กœ

โ—๏ธ   Subquery   ๐Ÿ‘‰  checkins c2 ํ…Œ์ด๋ธ” ์—์„œ, 

โ—๏ธ   where์ ˆ      ๐Ÿ‘‰  ์— ํ•ด๋‹นํ•˜๋Š” user_id๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์• ๋“ค๋งŒ ๊ฐ€์ ธ์™€์ค˜.

 

 

์‚ฌ์‹ค ์ด ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ group by ์ด์šฉํ•ด์„œ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

select c.checkin_id, 
       c.user_id, 
       c.likes, 
       avg(likes)  as avg_like_user
  from checkins c
 group by c.user_id

 

 

select ๋ฌธ์— subquery ๋„ฃ๊ณ  left join ๊นŒ์ง€ ํ•˜๋Š” ์˜ˆ์ œ.

select checkin_id, 
       c3.title, 
       user_id, 
       likes, 
       (
        select avg(c2.likes) from checkins c2
        where c.course_id = c2.course_id
       ) 
  from checkins c
  left join courses c3 on c.course_id = c3.course_id;

 

 

select ๋ฌธ์— subquery ๋ฅผ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด, 

ํ•œ์ค„ํ•œ์ค„ ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค, ๊ณ„์†ํ•ด์„œ ๊ทธ ์•ˆ์—์žˆ๋Š” subquery ๊ฐ€ ๋Œ์•„๊ฐ€๊ฒŒ ๋œ๋‹ค. 

 

 

 

 

from ์ ˆ ์•ˆ์—์„œ ์“ฐ์ธ  Subquery

๐Ÿ‘‰   ์ด๋ฏธ ์žˆ๋Š” ํ…Œ์ด๋ธ”์— subquery๋กœ ๋งŒ๋“  ํ…Œ์ด๋ธ”์„ join ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ.

๐Ÿ‘‰   ๋‚ด๊ฐ€ ์ถ”์ถœํ•œ ๋‚ด์šฉ๋งŒ ๋งˆ์น˜ ์ด๊ฒƒ๋งŒ ์žˆ๋Š” ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

 

select pu.user_id, a.avg_like, pu.point from point_users pu
inner join (
	select user_id, round(avg(likes),1) as avg_like from checkins
	group by user_id
) a on pu.user_id = a.user_id

 

 

 

 

 distinct( ํ•„๋“œ ์ด๋ฆ„)

๐Ÿ‘‰    ์ค‘๋ณต๋œ ์š”์†Œ ์ œ๊ฑฐ.

๐Ÿ‘‰    select ํ•„๋“œ์ด๋ฆ„, count(distinct(ํ•„๋“œ์ด๋ฆ„)) from ํ…Œ์ด๋ธ”

 

 

 

 

 

 with table1 as ( ์„œ๋ธŒ์ฟผ๋ฆฌ ), table2 as ( ์„œ๋ธŒ์ฟผ๋ฆฌ)

๐Ÿ‘‰    ์ค‘๋ณต๋œ ์š”์†Œ ์ œ๊ฑฐ.

๐Ÿ‘‰    select ํ•„๋“œ์ด๋ฆ„, count(distinct(ํ•„๋“œ์ด๋ฆ„)) from ํ…Œ์ด๋ธ”

๐Ÿ‘‰    table ์ค‘๋ณตํ•ด์„œ ์„ค์ •ํ•ด ์ค„ ๋•Œ์—๋Š” ์ฝค๋งˆ( , )๋ฅผ ์ด์šฉํ•œ๋‹ค.

 

with table1 as
  (
    select c.course_id, count(distinct(c.user_id)) as cnt_checkins from checkins c
    group by c.course_id
  ), table2 as (
    select o.course_id, count(distinct(o.user_id)) as cnt_total from orders o
    group by o.course_id
  )

select c.title,
       a.cnt_checkins,
       b.cnt_total,
       (a.cnt_checkins/b.cnt_total)as ratio
from table1 a
inner join table2 b on a.course_id = b.course_id
inner join courses c on b.course_id = c.course_id