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

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

by seolhee2750 2023. 6. 27.

์ตœ๊ทผ ์ถ”๊ฐ€๋œ Programmers์˜ Lv.2 ๋ฌธ์ œ๋“ค์˜ ํ’€์ด๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

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

 

๊ฐ€๊ฒฉ์ด ์ œ์ผ ๋น„์‹ผ ์‹ํ’ˆ์˜ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
select *
from food_product
where price in (select max(price) from food_product)

 

3์›”์— ํƒœ์–ด๋‚œ ์—ฌ์„ฑ ํšŒ์› ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ
select member_id, member_name, gender,
date_format(date_of_birth, '%Y-%m-%d') as date_of_birth
from member_profile
where month(date_of_birth) = '3' and gender = 'W' and tlno is not null
order by member_id

 

์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ์ƒํ’ˆ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
select left(product_code, 2) as category, count(*) as products
from product
group by category
order by category

 

๊ฐ€๊ฒฉ๋Œ€ ๋ณ„ ์ƒํ’ˆ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
select truncate(price, -4) as price_group, count(*) as products
from product
group by price_group
order by price_group

 

์ƒํ’ˆ ๋ณ„ ์˜คํ”„๋ผ์ธ ๋งค์ถœ ๊ตฌํ•˜๊ธฐ
select product_code, sum(p.price * o.sales_amount) sales
from product as p join offline_sale as o
on p.product_id = o.product_id
group by p.product_code
order by sales desc, product_code

 

์žฌ๊ตฌ๋งค๊ฐ€ ์ผ์–ด๋‚œ ์ƒํ’ˆ๊ณผ ํšŒ์› ๋ฆฌ์ŠคํŠธ ๊ตฌํ•˜๊ธฐ
select user_id, product_id
from online_sale
group by user_id, product_id
having count(*) >= 2
order by user_id, product_id desc

 

์ง„๋ฃŒ๊ณผ๋ณ„ ์ด ์˜ˆ์•ฝ ํšŸ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ
select mcdp_cd, count(*) as '5์›”์˜ˆ์•ฝ๊ฑด์ˆ˜'
from appointment
where date_format(apnt_ymd, '%Y-%m') = '2022-05'
group by mcdp_cd
order by 5์›”์˜ˆ์•ฝ๊ฑด์ˆ˜ asc, mcdp_cd asc

๐Ÿ–๏ธ ์œ„์—์„œ ์ƒˆ๋กญ๊ฒŒ ์ง€์ •ํ•ด์ค€ ์ปฌ๋Ÿผ๋ช…(ex. 5์›”์˜ˆ์•ฝ๊ฑด์ˆ˜)์„ order by์—์„œ ์“ธ ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ ์—†์ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

์„ฑ๋ถ„์œผ๋กœ ๊ตฌ๋ถ„ํ•œ ์•„์ด์Šคํฌ๋ฆผ ์ด ์ฃผ๋ฌธ๋Ÿ‰
select i.ingredient_type, sum(f.total_order) as total_order
from first_half as f join icecream_info as i
on f.flavor = i.flavor
group by i.ingredient_type
order by total_order

 

์กฐ๊ฑด์— ๋งž๋Š” ๋„์„œ์™€ ์ €์ž ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐ
select b.book_id, a.author_name, date_format(b.published_date, '%Y-%m-%d')
from book as b join author as a
on b.author_id = a.author_id
where b.category = '๊ฒฝ์ œ'
order by b.published_date

 

์ž๋™์ฐจ ์ข…๋ฅ˜ ๋ณ„ ํŠน์ • ์˜ต์…˜์ด ํฌํ•จ๋œ ์ž๋™์ฐจ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
select car_type, count(*) as cars
from car_rental_company_car
where options like '%ํ†ตํ’์‹œํŠธ%' or options like '%์—ด์„ ์‹œํŠธ%' or options like '%๊ฐ€์ฃฝ์‹œํŠธ%'
group by car_type
order by car_type

 

์ž๋™์ฐจ ํ‰๊ท  ๋Œ€์—ฌ ๊ธฐ๊ฐ„ ๊ตฌํ•˜๊ธฐ
select car_id, round(avg(datediff(end_date, start_date) + 1), 1) as average_duration
from car_rental_company_rental_history
group by car_id
having average_duration >= 7
order by average_duration desc, car_id desc

๐Ÿ–๏ธ ์–ด๋–ค ๊ธฐ๊ฐ„์˜ ๋‚  ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ๋Š”, ์‹œ์ž‘ํ•˜๋Š” ๋‚ ๊ณผ ๋๋‚˜๋Š” ๋‚ ์˜ ์ฐจ์— +1 ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์ž.

 

์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ค‘๊ณ ๊ฑฐ๋ž˜ ์ƒํƒœ ์กฐํšŒํ•˜๊ธฐ
select board_id, writer_id, title, price,
case
when status = 'DONE' then '๊ฑฐ๋ž˜์™„๋ฃŒ'
when status = 'SALE' then 'ํŒ๋งค์ค‘'
else '์˜ˆ์•ฝ์ค‘'
end
from used_goods_board
where created_date = '2022-10-05'
order by board_id desc

 

๋Œ“๊ธ€