๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/SQL

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐœ๋ฐœ์ž ์ฐพ๊ธฐ/MySQL - Lv.2

by The Future Engineer, Lucy 2024. 10. 31.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

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

 

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

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

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

SKILLCODES ํ…Œ์ด๋ธ”์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. SKILLCODES ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, NAME, CATEGORY, CODE๋Š” ๊ฐ๊ฐ ์Šคํ‚ฌ์˜ ์ด๋ฆ„, ์Šคํ‚ฌ์˜ ๋ฒ”์ฃผ, ์Šคํ‚ฌ์˜ ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์Šคํ‚ฌ์˜ ์ฝ”๋“œ๋Š” 2์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ ๊ฐ bit๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ๋„๋ก 2์˜ ์ œ๊ณฑ์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

NAME TYPE UNIQUE NULLABLE
NAME VARCHAR(N) Y N
CATEGORY VARCHAR(N) N N
CODE INTEGER Y N

DEVELOPERS ํ…Œ์ด๋ธ”์€ ๊ฐœ๋ฐœ์ž๋“ค์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํ‚ฌ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. DEVELOPERS ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ID, FIRST_NAME, LAST_NAME, EMAIL, SKILL_CODE๋Š” ๊ฐ๊ฐ ๊ฐœ๋ฐœ์ž์˜ ID, ์ด๋ฆ„, ์„ฑ, ์ด๋ฉ”์ผ, ์Šคํ‚ฌ ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. SKILL_CODE ์ปฌ๋Ÿผ์€ INTEGER ํƒ€์ž…์ด๊ณ , 2์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ ๊ฐ bit๋Š” SKILLCODES ํ…Œ์ด๋ธ”์˜ ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

NAME TYPE UNIQUE NULLABLE
ID VARCHAR(N) Y N
FIRST_NAME VARCHAR(N) N Y
LAST_NAME VARCHAR(N) N Y
EMAIL VARCHAR(N) Y N
SKILL_CODE INTEGER N N

์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ๊ฐœ๋ฐœ์ž์˜ SKILL_CODE๊ฐ€ 400 (=b'110010000')์ด๋ผ๋ฉด, ์ด๋Š” SKILLCODES ํ…Œ์ด๋ธ”์—์„œ CODE๊ฐ€ 256 (=b'100000000'), 128 (=b'10000000'), 16 (=b'10000') ์— ํ•ด๋‹นํ•˜๋Š” ์Šคํ‚ฌ์„ ๊ฐ€์กŒ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


๋ฌธ์ œ

DEVELOPERS ํ…Œ์ด๋ธ”์—์„œ Python์ด๋‚˜ C# ์Šคํ‚ฌ์„ ๊ฐ€์ง„ ๊ฐœ๋ฐœ์ž์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐœ๋ฐœ์ž์˜ ID, ์ด๋ฉ”์ผ, ์ด๋ฆ„, ์„ฑ์„ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

๊ฒฐ๊ณผ๋Š” ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด ์ฃผ์„ธ์š”.


์˜ˆ์‹œ

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

NAME CATEGORY CODE
C++ Back End 4
JavaScript Front End 16
Java Back End 128
Python Back End 256
C# Back End 1024
React Front End 2048
Vue Front End 8192
Node.js Back End 16384

DEVELOPERS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

ID FIRST_NAME LAST_NAME EMAIL SKILL_CODE
D165 Jerami Edwards jerami_edwards@grepp.co 400
D161 Carsen Garza carsen_garza@grepp.co 2048
D164 Kelly Grant kelly_grant@grepp.co 1024
D163 Luka Cory luka_cory@grepp.co 16384
D162 Cade Cunningham cade_cunningham@grepp.co 8452

๋‹ค์Œ๊ณผ ๊ฐ™์ด DEVELOPERS ํ…Œ์ด๋ธ”์— ํฌํ•จ๋œ ๊ฐœ๋ฐœ์ž ์ค‘ Python ์Šคํ‚ฌ์ด๋‚˜ C# ์Šคํ‚ฌ์„ ๊ฐ€์ง„ ๊ฐœ๋ฐœ์ž์˜ ์ •๋ณด๊ฐ€ ๊ฒฐ๊ณผ์— ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ID EMAIL FIST_NAME LAST_NAME
D162 cade_cunningham@grepp.co Cade Cunningham
D164 kelly_grant@grepp.co Kelly Grant
D165 jerami_edwards@grepp.co Jerami Edwards
  • D162๋ฒˆ ๊ฐœ๋ฐœ์ž์˜ ๊ฒฝ์šฐ SKILL_CODE๊ฐ€ 8452 = 8192 + 256 +4 ๋กœ Vue, Python, Cpp ์Šคํ‚ฌ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • D164๋ฒˆ ๊ฐœ๋ฐœ์ž์˜ ๊ฒฝ์šฐ SKILL_CODE๊ฐ€ 1024 ๋กœ C# ์Šคํ‚ฌ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • D165๋ฒˆ ๊ฐœ๋ฐœ์ž์˜ ๊ฒฝ์šฐ SKILL_CODE๊ฐ€ 400 = 256 + 128 + 16 ์œผ๋กœ Python, Java, JavaScript ์Šคํ‚ฌ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

โœ๐Ÿปํ’€์ด

bit_or๋ผ๋Š” aggregation function์„ ์‚ฌ์šฉํ•˜์—ฌ Python์ด๋‚˜ C# ์Šคํ‚ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
์ด๋ฅผ ๊ฐ ๊ฐœ๋ฐœ์ž๋“ค์˜ SKILL_CODE์™€ ๋น„๊ตํ•˜์—ฌ ๋Œ€์‘ํ•˜๋Š” ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ด๋ผ๋ฉด 1์„ ๋ฐ˜ํ™˜.
์ฆ‰, python๊ณผ C# ์Šคํ‚ฌ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ป์ฝ”๋“œ

select ID, EMAIL, FIRST_NAME, LAST_NAME
from DEVELOPERS
where SKILL_CODE & (select bit_or(CODE)
                   from SKILLCODES
                   where NAME = 'Python' or NAME = 'C#')
order by ID
728x90
๋ฐ˜์‘ํ˜•