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

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

by seolhee2750 2023. 6. 28.

2023๋…„ ์ƒˆ๋กญ๊ฒŒ ์—…๋ฐ์ดํŠธ๋œ Programmers Lv.3 ๋ฌธ์ œ๋“ค์„ ํ’€์ดํ–ˆ๋‹ค.

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

 

์กฐ๊ฑด๋ณ„๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ฃผ๋ฌธ์ƒํƒœ ์ถœ๋ ฅํ•˜๊ธฐ
select order_id, product_id, date_format(out_date, '%Y-%m-%d'),
(case
when out_date <= '2022-05-01' then '์ถœ๊ณ ์™„๋ฃŒ'
when out_date > '2022-05-01' then '์ถœ๊ณ ๋Œ€๊ธฐ'
else '์ถœ๊ณ ๋ฏธ์ •'
end) as ์ถœ๊ณ ์—ฌ๋ถ€
from food_order
order by order_id

๐Ÿ–๏ธ ๋‚ ์งœ๋ฅผ ๋น„๊ตํ•  ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

์ฆ๊ฒจ์ฐพ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‹๋‹น ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
select food_type, rest_id, rest_name, favorites
from rest_info
where (food_type, favorites) in (
    select food_type, max(favorites)
    from rest_info
    group by food_type
)
order by food_type desc

๐Ÿ–๏ธ group by ์ ˆ์„ ํ™œ์šฉํ•˜์—ฌ max ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ์ฃผ์˜์‚ฌํ•ญ

  • group by ์ ˆ ์‚ฌ์šฉ ์‹œ, ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” row๋“ค์€ ๋Œ€ํ‘œ๊ฐ€ ๋˜๋Š” ํ•˜๋‚˜์˜ row(์ตœ์ƒ๋‹จ)๋กœ ๋ฌถ์ธ๋‹ค.
  • ๋”ฐ๋ผ์„œ max ๊ฐ’์€ ๊ตฌํ•ด์ง€์ง€๋งŒ, ๊ทธ ์™ธ์˜ column๋“ค์ด ๊ตฌํ•œ max์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†๋‹ค.

-> max ๊ฐ’์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๋”ฐ๋กœ ๊ตฌํ•˜๊ณ , ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” row๋ฅผ ์ฐพ๋Š” ์‹์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์ž.

 

์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋„์„œ ํŒ๋งค๋Ÿ‰ ์ง‘๊ณ„ํ•˜๊ธฐ
select b.category, sum(s.sales) as total_sales
FROM book b join book_sales s on s.book_id = b.book_id 
where s.sales_date like '2022-01%'
group by b.category order by b.category

 

๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ์ž๋™์ฐจ๋“ค์˜ ์›”๋ณ„ ๋Œ€์—ฌ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ
select month(start_date) as month, car_id, count(*) as records
from car_rental_company_rental_history
where car_id in 
(select car_id 
from car_rental_company_rental_history
where date_format(start_date, '%Y-%m') between '2022-08' and '2022-10'
group by car_id
having count(car_id) >= 5
) and date_format(start_date, '%Y-%m') between '2022-08' and '2022-10'
group by month, car_id
having records > 0
order by month, car_id desc

 

์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก์—์„œ ๋Œ€์—ฌ์ค‘ / ๋Œ€์—ฌ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ๊ตฌ๋ถ„ํ•˜๊ธฐ
select car_id, if(car_id in (select car_id from car_rental_company_rental_history where '2022-10-16' between start_date and end_date), '๋Œ€์—ฌ์ค‘', '๋Œ€์—ฌ ๊ฐ€๋Šฅ') as availability
from car_rental_company_rental_history
group by car_id
order by car_id desc

 

๋Œ€์—ฌ ๊ธฐ๋ก์ด ์กด์žฌํ•˜๋Š” ์ž๋™์ฐจ ๋ฆฌ์ŠคํŠธ ๊ตฌํ•˜๊ธฐ
select distinct c.car_id
from car_rental_company_car as c join car_rental_company_rental_history as h
on c.car_id = h.car_id
where car_type = '์„ธ๋‹จ' and month(start_date) = 10
order by c.car_id desc

 

์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์šฉ์ž์™€ ์ด ๊ฑฐ๋ž˜๊ธˆ์•ก ์กฐํšŒํ•˜๊ธฐ
select user_id, nickname, sum(price) as total_sales
from used_goods_user as u join used_goods_board as b
on u.user_id = b.writer_id
where b.status = 'DONE'
group by user_id
having total_sales >= 700000
order by total_sales

 

์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ
select user_id, nickname, concat(city, ' ', street_address1, ' ', street_address2) as ์ „์ฒด์ฃผ์†Œ, concat(substr(tlno, 1, 3), '-', substr(tlno, 4, 4), '-', substr(tlno, 8, 4)) as ์ „ํ™”๋ฒˆํ˜ธ
from used_goods_user as u join used_goods_board as b
on u.user_id = b.writer_id
group by user_id
having count(*) >= 3
order by user_id desc

๐Ÿ–๏ธ substr ์‚ฌ์šฉ ์‹œ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค(1๋ถ€ํ„ฐ ์‹œ์ž‘)์™€ ๊ฐ€์ ธ์˜ฌ ๊ธ€์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์กฐํšŒ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์ค‘๊ณ ๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ์˜ ์ฒจ๋ถ€ํŒŒ์ผ ์กฐํšŒํ•˜๊ธฐ
select concat('/home/grep/src/', f.board_id, '/', f.file_id, f.file_name, f.file_ext) as file_path
from used_goods_file as f join used_goods_board as b
on f.board_id = b.board_id
where b.views = (select max(views) from used_goods_board)
order by file_path desc

 

๋Œ“๊ธ€