๋ฐ˜์‘ํ˜•

๐Ÿ’ SQL (Programmers)/Group By 23

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ์ž๋™์ฐจ๋“ค์˜ ์›”๋ณ„ ๋Œ€์—ฌ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/151139 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ 1. CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์—์„œ 2. ๋Œ€์—ฌ ์‹œ์ž‘์ผ์„ ๊ธฐ์ค€์œผ๋กœ 2022๋…„ 8์›”๋ถ€ํ„ฐ 2022๋…„ 10์›”๊นŒ์ง€ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ 5ํšŒ ์ด์ƒ์ธ ์ž๋™์ฐจ๋“ค์— ๋Œ€ํ•ด์„œ 3. ํ•ด๋‹น ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ์›”๋ณ„ ์ž๋™์ฐจ ID ๋ณ„ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜(์ปฌ๋Ÿผ๋ช…: RECORDS) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. 4. ๊ฒฐ๊ณผ๋Š” ์›”์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , ์›”์ด ๊ฐ™๋‹ค๋ฉด ์ž๋™์ฐจ ID๋ฅผ..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก์—์„œ ๋Œ€์—ฌ์ค‘ / ๋Œ€์—ฌ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ๊ตฌ๋ถ„ํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/157340 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์—์„œ 2022๋…„ 10์›” 16์ผ์— ๋Œ€์—ฌ ์ค‘์ธ ์ž๋™์ฐจ์ธ ๊ฒฝ์šฐ '๋Œ€์—ฌ์ค‘' ์ด๋ผ๊ณ  ํ‘œ์‹œํ•˜๊ณ , 1. ๋Œ€์—ฌ ์ค‘์ด์ง€ ์•Š์€ ์ž๋™์ฐจ์ธ ๊ฒฝ์šฐ '๋Œ€์—ฌ ๊ฐ€๋Šฅ'์„ ํ‘œ์‹œํ•˜๋Š” ์ปฌ๋Ÿผ (์ปฌ๋Ÿผ๋ช…: AVAILABILITY)์„ ์ถ”๊ฐ€ํ•˜์—ฌ 2. ์ž๋™์ฐจ ID์™€ AVAILABILITY ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๋ฐ˜๋‚ฉ ๋‚ ์งœ๊ฐ€ 2022๋…„ 10์›” 16..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ํ—ค๋น„ ์œ ์ €๊ฐ€ ์†Œ์œ ํ•œ ์žฅ์†Œ

https://school.programmers.co.kr/learn/courses/30/lessons/77487 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ ์ด ์„œ๋น„์Šค์—์„œ๋Š” ๊ณต๊ฐ„์„ ๋‘˜ ์ด์ƒ ๋“ฑ๋กํ•œ ์‚ฌ๋žŒ์„ "ํ—ค๋น„ ์œ ์ €"๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ—ค๋น„ ์œ ์ €๊ฐ€ ๋“ฑ๋กํ•œ ๊ณต๊ฐ„์˜ ์ •๋ณด๋ฅผ ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ํ’€์ด -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” select * from PLACES where HOST_ID in ( SELECT HOST_ID from PLACES group by HOST_ID having count(HOST_ID) >= 2 ) ord..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ์ฆ๊ฒจ์ฐพ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‹๋‹น ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/131123 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ์ฒ˜์Œ์—๋Š” ๋ณ„ ์ƒ๊ฐ์—†์ด ์•„์ฃผ ์‰ฝ๊ฒŒ ์ฟผ๋ฆฌ๋ฅผ ์งฐ๋Š”๋ฐ SELECT FOOD_TYPE, REST_ID, REST_NAME, max(FAVORITES) as FAVORITES from REST_INFO group by FOOD_TYPE ํ‹€๋ ธ๋‹ค. ์•Œ๊ณ ๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด FOOD_TYPE ์ข…๋ฅ˜๋ณ„๋กœ ๊ฐ€์žฅ FAVORITES๊ฐ€ ํฐ FOOD_TYPE๊ณผ FAVORITES ๊ฐ’์€ ๋ฐ˜ํ™˜ํ•ด์ฃผ์ง€๋งŒ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” REST_ID๋‚˜ ..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์šฉ์ž์™€ ์ด ๊ฑฐ๋ž˜๊ธˆ์•ก ์กฐํšŒํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/164668 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” SELECT USER_ID, NICKNAME, sum(PRICE) as TOTAL_SALES from USED_GOODS_BOARD b join USED_GOODS_USER u on b.WRITER_ID = u.USER_ID where STATUS = 'DONE' group by USER_ID having TOTAL_SALES >= 700000 order by TOTAL_..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  Kit: ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋„์„œ ํŒ๋งค๋Ÿ‰ ์ง‘๊ณ„ํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/144855 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” select CATEGORY, sum(SALES) as TOTAL_SALES from BOOK b join BOOK_SALES s on b.BOOK_ID = s.BOOK_ID where year(SALES_DATE) = 2022 and month(SALES_DATE) = 01 group by CATEGORY order by CATEGORY

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  Kit: ๊ฐ€๊ฒฉ๋Œ€ ๋ณ„ ์ƒํ’ˆ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/131530 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ์ฒ˜์Œ์—๋Š” ํ’€์ด2๋กœ ํ’€์—ˆ์œผ๋‚˜ ๋„ˆ๋ฌด ๋…ธ๊ฐ€๋‹ค๋ผ๋Š” ์ƒ๊ฐ์— ํ’€์ด1๋กœ ํ’€์—ˆ๋‹ค ํ’€์ด1 ๋‚ด๋ฆผ ์—ฐ์‚ฐ (floor)์„ ์ด์šฉํ•œ ํ’€์ด -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” SELECT floor(PRICE/10000)*10000 as PRICE_GROUP, count(*) as PRODUCTS from PRODUCT group by PRICE_GROUP order by PRICE_GROUP asc ํ’€์ด2 case when then..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  kit: ์„ฑ๋ถ„์œผ๋กœ ๊ตฌ๋ถ„ํ•œ ์•„์ด์Šคํฌ๋ฆผ ์ด ์ฃผ๋ฌธ๋Ÿ‰

https://school.programmers.co.kr/learn/courses/30/lessons/133026 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” SELECT INGREDIENT_TYPE, sum(TOTAL_ORDER) as TOTAL_ORDER from FIRST_HALF f join ICECREAM_INFO i on f.FLAVOR = i.FLAVOR group by INGREDIENT_TYPE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  Kit: ์ž๋™์ฐจ ์ข…๋ฅ˜ ๋ณ„ ํŠน์ • ์˜ต์…˜์ด ํฌํ•จ๋œ ์ž๋™์ฐจ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/151137 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” 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 asc

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  Kit : ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/59412 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” SELECT hour(DATETIME) as HOUR, count(DATETIME) as COUNT from ANIMAL_OUTS where hour(DATETIME) >= 9 and hour(DATETIME) < 20 group by HOUR order by HOUR

๋ฐ˜์‘ํ˜•