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