๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
6๏ธโƒฃ SQL/Problem Solving

[MySQL] Programmers Lv.4, 5 ์ „์ฒด ๋ฌธ์ œ ํ’€์ด (2023 ์—…๋ฐ์ดํŠธ!)

by seolhee2750 2023. 6. 29.

23๋…„ ์—…๋ฐ์ดํŠธ๋œ Lv.4, 5 ๋ฌธ์ œ๋“ค๊นŒ์ง€ ๋ชจ๋‘ ํ’€์–ด๋ณด์•˜๋‹ค. :)

 

Programmers์˜ ๋ชจ๋“  SQL ๋ฌธ์ œ๋“ค์„ ํ’€์–ด๋ณธ ํ›„๊ธฐ๋Š”!

ํ™•์‹คํžˆ ์ตœ๊ทผ ์˜ฌ๋ผ๊ฐ„ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋‚œ์ด๋„์— ๋งž์ถฐ์„œ ์กฐ๊ธˆ ๋” ์ˆ˜์ค€๋†’์€ ๋ฌธ์ œ๋“ค์ด ์ƒ๊ธด ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ „์—๋Š” concat, substr, round, truncate, datediff์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ๋‚˜

์„ธ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜๋Š” ๋ฌธ์ œ, ๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ปฌ๋Ÿผ์œผ๋กœ group by ํ•˜๋Š” ๋ฌธ์ œ ๋“ฑ

๋ฌธ์ œ์˜ ๋‹ค์–‘์„ฑ์ด ๋งค์šฐ ๋ถ€์กฑํ•ด์„œ Programmers ๋งŒ์œผ๋กœ SQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ๋Œ€๋น„ํ•˜๊ธฐ์—๋Š” ๋ฌด๋ฆฌ๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

์ด์ œ๋Š” ์ƒˆ๋กญ๊ฒŒ ์—…๋ฐ์ดํŠธ๋œ ๋ฌธ์ œ๋“ค๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ํ’€์–ด๋ณธ๋‹ค๋ฉด, SQL ๋ฌธ์ œ ํ’€์ด ๋Œ€๋น„์— ์ถฉ๋ถ„ํ•˜๋‹ค๊ณ  ๋Š๊ปด์ง„๋‹ค.

์งฑ! ๐Ÿ‘

 

๊ธฐ์กด ๋ฌธ์ œ๋“ค์˜ ํ’€์ด๋Š” ๐Ÿ‘‰์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š” !

 

์‹ํ’ˆ๋ถ„๋ฅ˜๋ณ„ ๊ฐ€์žฅ ๋น„์‹ผ ์‹ํ’ˆ์˜ ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ
select category, price, product_name
from food_product
where category in ('๊ณผ์ž', '๊ตญ', '๊น€์น˜', '์‹์šฉ์œ ')
and (category, price) in (select category, max(price)
                           from food_product
                           group by category)
order by price desc

 

5์›” ์‹ํ’ˆ๋“ค์˜ ์ด๋งค์ถœ ์กฐํšŒํ•˜๊ธฐ
select p.product_id, p.product_name, sum(o.amount * p.price) as total_sales
from food_order as o join food_product as p
on o.product_id = p.product_id
where substr(produce_date, 1, 7) = '2022-05'
group by p.product_id
order by total_sales desc, p.product_id

 

์„œ์šธ์— ์œ„์น˜ํ•œ ์‹๋‹น ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ
select i.rest_id, rest_name, food_type, favorites, address, round(avg(review_score), 2) as score
from rest_info as i join rest_review as r
on i.rest_id = r.rest_id
where substr(address, 1, 2) = '์„œ์šธ'
group by i.rest_id
order by score desc, favorites desc

 

๊ทธ๋ฃน๋ณ„ ์กฐ๊ฑด์— ๋งž๋Š” ์‹๋‹น ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ
select member_name, review_text, date_format(review_date, '%Y-%m-%d')
from member_profile as p join rest_review as r
on p.member_id = r.member_id
where p.member_id = (select member_id
                    from rest_review
                    group by member_id
                    order by count(*) desc
                    limit 1)
order by review_date, review_text

 

๋…„, ์›”, ์„ฑ๋ณ„ ๋ณ„ ์ƒํ’ˆ ๊ตฌ๋งค ํšŒ์› ์ˆ˜ ๊ตฌํ•˜๊ธฐ
select year(s.sales_date) as year, month(s.sales_date) as month, gender, count(distinct s.user_id) users
from user_info i join online_sale s on i.user_id = s.user_id
where gender is not null
group by year, month, gender
order by year asc, month asc, gender asc

 

์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ํšŒ์› ๋น„์œจ ๊ตฌํ•˜๊ธฐ
select year(sales_date) as year, month(sales_date) as month, count(distinct u.user_id) as puchased_users, round((count(distinct u.user_id) / (select count(*) from user_info where year(joined) = 2021)), 1) as puchased_ratio
from (select * from user_info where year(joined) = 2021) as u
join online_sale as o
on u.user_id = o.user_id
group by year, month
order by year, month

 

์˜คํ”„๋ผ์ธ/์˜จ๋ผ์ธ ํŒ๋งค ๋ฐ์ดํ„ฐ ํ†ตํ•ฉํ•˜๊ธฐ
select date_format(sales_date, "%Y-%m-%d") as sales_date, product_id, user_id, sales_amount
from online_sale 
where month(sales_date) = 3

union all

select date_format(sales_date, "%Y-%m-%d") as sales_date, product_id, null as user_id, sales_amount
from offline_sale
where month(sales_date) = 3

order by sales_date, product_id, user_id

 

์ทจ์†Œ๋˜์ง€ ์•Š์€ ์ง„๋ฃŒ ์˜ˆ์•ฝ ์กฐํšŒํ•˜๊ธฐ
select x.apnt_no, p.pt_name, x.pt_no, x.mcdp_cd, x.dr_name, x.apnt_ymd
from patient as p
join (select a.apnt_ymd, a.apnt_no, a.pt_no, a.mcdp_cd, a.mddr_id, a.apnt_cncl_yn, a.apnt_cncl_ymd, d.dr_name
     from appointment as a
     join doctor as d
     on a.mddr_id = d.dr_id
     where date_format(apnt_ymd, '%Y-%m-%d') = '2022-04-13' 
      and apnt_cncl_yn = 'N') as x
on p.pt_no = x.pt_no
order by x.apnt_ymd

 

์ฃผ๋ฌธ๋Ÿ‰์ด ๋งŽ์€ ์•„์ด์Šคํฌ๋ฆผ๋“ค ์กฐํšŒํ•˜๊ธฐ
select f.flavor
from first_half as f join july as j
on f.flavor = j.flavor
group by f.flavor
order by sum(f.total_order) desc
limit 3

 

์ €์ž ๋ณ„ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋งค์ถœ์•ก ์ง‘๊ณ„ํ•˜๊ธฐ
select b.author_id, a.author_name, b.category, sum(b.price * s.sales)
from book as b join author as a
on b.author_id = a.author_id join (select * from book_sales where substr(sales_date, 1, 7) = '2022-01') as s
on b.book_id = s.book_id
group by b.author_id, b.category
order by b.author_id, b.category desc

 

์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ๋ณ„ ๋Œ€์—ฌ ๊ธˆ์•ก ๊ตฌํ•˜๊ธฐ
select history_id, truncate((datediff(end_date, start_date) + 1) * daily_fee
                         * (case
                            when (datediff(end_date, start_date) + 1) < 7 then 1
                            when (datediff(end_date, start_date) + 1) < 30 then 0.95
                            when (datediff(end_date, start_date) + 1) < 90 then 0.92
                            else 0.85
                            end), 0) as fee
from car_rental_company_car as c join car_rental_company_rental_history as h
on c.car_id = h.car_id
where c.car_type = 'ํŠธ๋Ÿญ'
order by fee desc, history_id desc

 

ํŠน์ • ๊ธฐ๊ฐ„๋™์•ˆ ๋Œ€์—ฌ ๊ฐ€๋Šฅํ•œ ์ž๋™์ฐจ๋“ค์˜ ๋Œ€์—ฌ๋น„์šฉ ๊ตฌํ•˜๊ธฐ
select distinct c.car_id, c.car_type, truncate(daily_fee * ((100 - p.discount_rate) * 0.01) * 30, 0) as fee
from car_rental_company_car as c
join car_rental_company_rental_history as h on c.car_id = h.car_id
join car_rental_company_discount_plan as p on c.car_type = p.car_type
where c.car_type in ('์„ธ๋‹จ', 'SUV')
and c.car_id not in (select car_id
                     from car_rental_company_rental_history
                     where end_date >= '2022-11-01' 
                     and start_date <= '2022-11-30') and p.duration_type = '30์ผ ์ด์ƒ'
having fee between 500000 and (2000000 - 1)
order by fee desc, c.car_type, c.car_id desc

 

๋Œ“๊ธ€