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