๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(SQL)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_์ธ๊ธฐ์žˆ๋Š” ์•„์ด์Šคํฌ๋ฆผ

by carrot0911 2024. 11. 15.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/133024

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

FIRST_HALF ํ…Œ์ด๋ธ”์€ ์•„์ด์Šคํฌ๋ฆผ ๊ฐ€๊ฒŒ์˜ ์ƒ๋ฐ˜๊ธฐ ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. FIRST_HALF ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, SHIPMENT_IDFLAVORTOTAL_ORDER๋Š” ๊ฐ๊ฐ ์•„์ด์Šคํฌ๋ฆผ ๊ณต์žฅ์—์„œ ์•„์ด์Šคํฌ๋ฆผ ๊ฐ€๊ฒŒ๊นŒ์ง€์˜ ์ถœํ•˜ ๋ฒˆํ˜ธ, ์•„์ด์Šคํฌ๋ฆผ ๋ง›, ์ƒ๋ฐ˜๊ธฐ ์•„์ด์Šคํฌ๋ฆผ ์ด์ฃผ๋ฌธ๋Ÿ‰์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

NAME TYPE NULLABLE
SHIPMENT_ID INT(N) FALSE
FLAVOR VARCHAR(N) FALSE
TOTAL_ORDER INT(N) FALSE

๋ฌธ์ œ

์ƒ๋ฐ˜๊ธฐ์— ํŒ๋งค๋œ ์•„์ด์Šคํฌ๋ฆผ์˜ ๋ง›์„ ์ด์ฃผ๋ฌธ๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ์ด์ฃผ๋ฌธ๋Ÿ‰์ด ๊ฐ™๋‹ค๋ฉด ์ถœํ•˜ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด FIRST_HALF ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๋•Œ

SHIPMENT_ID FLAVOR TOTAL_ORDER
101 chocolate 3200
102 vanilla 2800
103 mint_chocolate 1700
104 caramel 2600
105 white_chocolate 3100
106 peach 2450
107 watermelon 2150
108 mango 2900
109 strawberry 3100
110 melon 3150
111 orange 2900
112 pineapple 2900

์ƒ๋ฐ˜๊ธฐ ์•„์ด์Šคํฌ๋ฆผ ๋ง›์„ ์ด์ฃผ๋ฌธ๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ์ด์ฃผ๋ฌธ๋Ÿ‰์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์ถœํ•˜ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉด chocolate, melon, white_chocolate, strawberry, mango, orange, pineapple, vanilla, caramel, peach, watermelon, mint_chocolate ์ˆœ์„œ๋Œ€๋กœ ์กฐํšŒ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

FLAVOR
chocolate
melon
white_chocolate
strawberry
mango
orange
pineapple
caramel
peach
watermelon
mint_chocolate 


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

SELECT FLAVOR
FROM FIRST_HALF
ORDER BY TOTAL_ORDER DESC, SHIPMENT_ID ASC;

์ฝ”๋“œ ์„ค๋ช…

  • SELECT FLAVOR : FLAVOR ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค.
  • FROM FIRST_HALF : FIRST_HALF ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  • ORDER BY TOTAL_ORDER DESC, SHIPMENT_ID ASC : TOTAL_ORDER ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , ๋™์ผํ•˜๋‹ค๋ฉด SHIPMENT_ID ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.