๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DBMS/Mariadb

[DBMS]ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฅผ ์œ„ํ•œ MariaDB ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ๋ฒ•

by YJ Dev 2024. 6. 1.
728x90
๋ฐ˜์‘ํ˜•
SMALL

์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ์กฐํšŒ์™€ ๋ถ„์„์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์ค‘์š”ํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” MariaDB ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๊ฐœ๋…, ์ข…๋ฅ˜, ์ž‘์„ฑ ๋ฐ ์‚ฌ์šฉ๋ฒ•, ์‹ค์ „ ์˜ˆ์ œ, ์„ฑ๋Šฅ ์ตœ์ ํ™” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

sql ์„œ๋ธŒ์ฟผ๋ฆฌ

์˜ˆ์ œ ๋ฐ์ดํ„ฐ๐Ÿ“ฆ

์•„๋ž˜๋Š” ๊ฐ ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ œ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค:

Employees ํ…Œ์ด๋ธ”

employee_id name department_id salary
1 Alice 101 50000
2 Bob 102 60000
3 Charlie 101 55000
4 David 103 45000
5 Eve 102 70000


Departments ํ…Œ์ด๋ธ”

department_id name location_id
101 Sales 1700
102 IT 1700
103 HR 1800


Locations ํ…Œ์ด๋ธ”

location_id city
1700 London
1800 Paris


Job_History ํ…Œ์ด๋ธ”

department_id job_id end_date
101 1 2023-03-15
102 2 2024-01-01
103 3 2022-12-31

์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€ ๋ฌด์—‡์ธ๊ฐ€โ“

์ •์˜: ์„œ๋ธŒ์ฟผ๋ฆฌ(Subquery)๋Š” SQL ๋ฌธ ์•ˆ์— ํฌํ•จ๋œ ๋˜ ๋‹ค๋ฅธ SQL ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ œํ•œํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์šฉ๋„: ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰, ์กฐ๊ฑด๋ถ€ ๋ฐ์ดํ„ฐ ์กฐํšŒ, ์ง‘๊ณ„ ํ•จ์ˆ˜์™€ ๊ฒฐํ•ฉ๋œ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ

SELECT * 
FROM `employees` 
WHERE `salary` > (
    SELECT AVG(`salary`) 
    FROM `employees`
);

์œ„ ์˜ˆ์ œ๋Š” ์ „์ฒด ์ง์›์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋ณด๋‹ค ๋†’์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์›์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ

employee_id name department_id salary
2 Bob 102 60000
3 Charlie 101 55000
4 Eve 102 70000

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜๐Ÿ“Š

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ๊ฐ’์ด๋‚˜ ํ•˜๋‚˜์˜ ํ–‰๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๋Š” WHERE ๊ตฌ๋ฌธ์ด๋‚˜ SELECT ๊ตฌ๋ฌธ์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์žฅ ๋†’์€ ๊ฐ’์„ ์ฐพ๊ฑฐ๋‚˜ ์กฐ๊ฑด์— ๋งž๋Š” ํ•œ ๊ฐ€์ง€ ๊ฐ’์„ ์ฐพ๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ

Sales ๋ถ€์„œ์— ์†ํ•œ ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name` 
FROM `employees` 
WHERE `department_id` = (
    SELECT `department_id` 
    FROM `departments` 
    WHERE `name` = 'Sales'
);


๊ฒฐ๊ณผ

name
Alice
Bob

๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๋Š” IN, ANY, ALL ๋“ฑ์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ์ง‘ํ•ฉ์— ํฌํ•จ๋˜๋Š” ๊ฐ’๋“ค์„ ์ฐพ๊ฑฐ๋‚˜, ์—ฌ๋Ÿฌ ํ–‰์„ ๋น„๊ตํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ

London์— ์œ„์น˜ํ•œ ๋ถ€์„œ์— ์†ํ•œ ์ง์›์˜ ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name` 
FROM `employees` 
WHERE `department_id` IN (
    SELECT `department_id` 
    FROM `departments` 
    WHERE `location_id` = (
        SELECT `location_id` 
        FROM `locations` 
        WHERE `city` = 'London'
    )
);


๊ฒฐ๊ณผ

name
Alice
Bob
Charlie
Eve

IN, ALL, ANY๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋‚˜ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ํŠน์ • ๊ฐ’์„ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์—ญํ• ์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค:

  1. IN: IN ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ์•ˆ์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, WHERE column_name IN (subquery)์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜์–ด column_name์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ANY: ANY ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ์ค‘ ํ•˜๋‚˜ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, WHERE column_name > ANY (subquery)์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜์–ด column_name์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ์ค‘ ์–ด๋–ค ๊ฐ’๋ณด๋‹ค ํฐ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ALL: ALL ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๊ฐ’์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, WHERE column_name > ALL (subquery)์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜์–ด column_name์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ์•ˆ์˜ ๋ชจ๋“  ๊ฐ’๋ณด๋‹ค ํฐ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด IN, ALL, ANY ์—ฐ์‚ฐ์ž๋ฅผ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์˜ˆ์ œ

ํ•™์ƒ๋“ค์˜ ์„ฑ์ ์„ ์ €์žฅํ•œ ํ…Œ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ…Œ์ด๋ธ”์—์„œ ํŠน์ • ๊ณผ๋ชฉ์˜ ์„ฑ์ ์ด ํŠน์ • ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

students ํ…Œ์ด๋ธ”

student_id name score
1 Alice 85
2 Bob 70
3 Charlie 95
4 David 60
5 Eve 75

1. IN ์—ฐ์‚ฐ์ž ์˜ˆ์ œ:

SELECT `name` 
FROM `students` 
WHERE `score` IN (85, 95);

์ด ์ฟผ๋ฆฌ๋Š” ์„ฑ์ ์ด 85 ๋˜๋Š” 95์ธ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” "Alice"์™€ "Charlie"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


2. ANY ์—ฐ์‚ฐ์ž ์˜ˆ์ œ:

SELECT name 
FROM students 
WHERE score > ANY (SELECT score FROM students WHERE name = 'Bob');

์ด ์ฟผ๋ฆฌ๋Š” "Bob"์˜ ์„ฑ์ ๋ณด๋‹ค ๋†’์€ ์„ฑ์ ์„ ๋ฐ›์€ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” "Alice", "Charlie", "Eve"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

3. ALL ์—ฐ์‚ฐ์ž ์˜ˆ์ œ:

SELECT name 
FROM students 
WHERE score > ALL (SELECT score FROM students WHERE name = 'David');

์ด ์ฟผ๋ฆฌ๋Š” "David"์˜ ์„ฑ์ ๋ณด๋‹ค ๋†’์€ ์„ฑ์ ์„ ๋ชจ๋‘ ๋ฐ›์€ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” "Alice", "Charlie", "Eve"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์ฃผ๋กœ ์™ธ๋ถ€ ์ฟผ๋ฆฌ์—์„œ ๋ณต์ˆ˜์˜ ์—ด ๊ฐ’์„ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•  ๋•Œ ๋‹ค์ค‘ ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ˆ์ œ

'2023-01-01' ์ดํ›„์— ์ข…๋ฃŒ๋œ ๋ถ€์„œ์™€ ์ง๋ฌด์— ์†ํ•œ ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name` 
FROM `employees` 
WHERE (`department_id`, `job_id`) IN (
    SELECT `department_id`, `job_id` 
    FROM `job_history` 
    WHERE `end_date` > '2023-01-01'
);


๊ฒฐ๊ณผ

name
Charlie
Eve

์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ

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

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


์˜ˆ์ œ

๊ฐ ๋ถ€์„œ์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋ณด๋‹ค ๋†’์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name` 
FROM `employees` AS `e` 
WHERE `salary` > (
    SELECT AVG(`salary`) 
    FROM `employees` 
    WHERE `department_id` = `e`.`department_id`
);


๊ฒฐ๊ณผ

name
Bob
Eve

์„œ๋ธŒ์ฟผ๋ฆฌ ์ž‘์„ฑ ๋ฐ ์‚ฌ์šฉ๋ฒ•๐Ÿ“

1. ๊ธฐ๋ณธ ๋ฌธ๋ฒ•: ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๊ด„ํ˜ธ ()๋กœ ๊ฐ์‹ธ์„œ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

2. SELECT ๋ฌธ ๋‚ด ์„œ๋ธŒ์ฟผ๋ฆฌ:

์˜ˆ์ œ

๊ฐ ์ง์›์˜ ํ‰๊ท  ๊ธ‰์—ฌ์™€ ํ•จ๊ป˜ ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name`, 
    (SELECT AVG(`salary`) FROM `employees`) AS `avg_salary` 
FROM `employees`;


๊ฒฐ๊ณผ

name avg_salary
Alice 56000
Bob 56000
Charlie 56000
David 56000
Eve 56000


3. WHERE ์ ˆ ๋‚ด ์„œ๋ธŒ์ฟผ๋ฆฌ:

์˜ˆ์ œ

HR ๋ถ€์„œ์— ์†ํ•œ ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `name` 
FROM `employees` 
WHERE `department_id` = (
    SELECT `department_id` 
    FROM `departments` 
    WHERE `name` = 'HR'
);


๊ฒฐ๊ณผ

name
David


4. FROM ์ ˆ ๋‚ด ์„œ๋ธŒ์ฟผ๋ฆฌ:

์˜ˆ์ œ

๊ฐ ์ง์›์˜ ๋ถ€์„œ ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ์ง์› ์ด๋ฆ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SELECT `e`.`name`, `d`.`department_name` 
FROM `employees` AS `e`
JOIN (
    SELECT `department_id`, `name` AS `department_name` 
    FROM `departments`
) AS `d` ON `e`.`department_id` = `d`.`department_id`;


๊ฒฐ๊ณผ

name department_name
Alice Sales
Bob IT
Charlie Sales
David HR
Eve IT

์„ฑ๋Šฅ ์ตœ์ ํ™”๐Ÿš€

1. ์„œ๋ธŒ์ฟผ๋ฆฌ ์„ฑ๋Šฅ ๋ฌธ์ œ: ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋•Œ๋•Œ๋กœ ์„ฑ๋Šฅ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ๊ฐ ํ–‰๋งˆ๋‹ค ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ์ตœ์ ํ™” ํŒ:

  • ์ธ๋ฑ์Šค ์‚ฌ์šฉ: ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ด์— ์ธ๋ฑ์Šค๋ฅผ ์„ค์ •ํ•˜์—ฌ ์กฐํšŒ ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค.
  • JOIN ์‚ฌ์šฉ: ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์„œ๋ธŒ์ฟผ๋ฆฌ ๋Œ€์‹  JOIN์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒํ•ฉ๋‹ˆ๋‹ค.

3. ๋Œ€์•ˆ ์ œ์‹œ: JOIN์„ ์‚ฌ์šฉํ•œ ๋Œ€์ฒด ๋ฐฉ๋ฒ•

SELECT `e`.`name`, `d`.`name` 
FROM `employees` AS `e` 
JOIN `departments` AS `d` ON `e`.`department_id` = `d`.`department_id` 
WHERE `d`.`location_id` = 1700;

์œ„์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค:

  1. employees ํ…Œ์ด๋ธ”๊ณผ departments ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด employees ํ…Œ์ด๋ธ”์„ e๋ผ๋Š” ๋ณ„์นญ์œผ๋กœ, departments ํ…Œ์ด๋ธ”์„ d๋ผ๋Š” ๋ณ„์นญ์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. ์กฐ์ธ ์กฐ๊ฑด์œผ๋กœ๋Š” employees ํ…Œ์ด๋ธ”์˜ department_id์™€ departments ํ…Œ์ด๋ธ”์˜ department_id๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. ์กฐ๊ฑด์ ˆ์—์„œ๋Š” departments ํ…Œ์ด๋ธ”์˜ location_id๊ฐ€ 1700์ธ ํ–‰๋งŒ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  4. ๊ฒฐ๊ณผ๋กœ๋Š” employees ํ…Œ์ด๋ธ”์˜ ์ง์› ์ด๋ฆ„๊ณผ ๊ทธ๋“ค์ด ์†ํ•œ ๋ถ€์„œ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ

e.name d.name
Alice Sales
Bob IT
Charlie Sales
Eve IT

ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

sql ์„œ๋ธŒ์ฟผ๋ฆฌ
728x90
๋ฐ˜์‘ํ˜•