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
'6๏ธโฃ SQL > Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] Programmers Lv.4, 5 ์ ์ฒด ๋ฌธ์ ํ์ด (2023 ์ ๋ฐ์ดํธ!) (0) | 2023.06.29 |
---|---|
[MySQL] Programmers Lv.2 ์ ์ฒด ๋ฌธ์ ํ์ด (2023 ์ ๋ฐ์ดํธ!) (1) | 2023.06.27 |
[MySQL] Programmers Lv.1 ์ ์ฒด ๋ฌธ์ ํ์ด (2023 ์ ๋ฐ์ดํธ!) (2) | 2023.04.26 |
[MySQL] Programmers Lv.4 ์ ์ฒด ๋ฌธ์ ํ์ด (0) | 2022.02.02 |
[MySQL] Programmers Lv.3 ์ ์ฒด ๋ฌธ์ ํ์ด (0) | 2022.02.02 |
๋๊ธ