[SQL] ๊ณ ๊ธ‰ ๋ฌธ๋ฒ•๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

Arielยท2024๋…„ 4์›” 9์ผ
0

SQL

๋ชฉ๋ก ๋ณด๊ธฐ
3/3

5. SQL ๊ณ ๊ธ‰ ๋ฌธ๋ฒ•


  • SELECT (ํ…Œ์ด๋ธ” ์กฐํšŒ)
    1. ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ
    ๐Ÿ’ก๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

    [table1] test.information

    nonameage
    1Gildong16
    2Kongjwi17
    3Michael14
    4Ariel16

    [table2] test.score

    nonamesubjectscore
    1ArielMath95
    2GildongKorean40


    1-1. Left join
    ๐Ÿ’ก๊ธฐ์ค€ ํ…Œ์ด๋ธ”๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ join ํ…Œ์ด๋ธ”์—์„œ ๊ฒฐํ•ฉ๋˜๋ฉฐ ์—†๋Š” ๊ฐ’์€ null ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ ธ ๊ฒฐํ•ฉ

     
    -- Left join
    -- information(A)์— score(B) ์ •๋ณด ๊ฒฐํ•ฉ(key: name)
     select
     	t1.*
     	, t2.subject
     	, t2.score
     from test.information as t1
     left join test.score  as t2 on t1.name = t2.name
    [๊ฒฐ๊ณผ] Left join
    nonameagesubjectscore
    1Gildong16Korean40
    2Kongjwi17
    3Michael14
    4Ariel16Math95

    1-2. Right join
    ๐Ÿ’กjoin ํ…Œ์ด๋ธ”๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์—์„œ ๊ฒฐํ•ฉ๋˜๋ฉฐ ์—†๋Š” ๊ฐ’์€ null ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ ธ ๊ฒฐํ•ฉ

     
    -- Right join
    -- score(B)์— information(A) ์ •๋ณด ๊ฒฐํ•ฉ(key: name)
     select
     	t2.*
     	, t2.age
     from test.information as t1
     right join test.score as t2 on t1.name = t2.name
    [๊ฒฐ๊ณผ] Right join
    nonamesubjectscoreage
    2GildongKorean4016
    1ArielMath9516

    1-3. Inner join
    ๐Ÿ’ก๊ธฐ์ค€ ํ…Œ์ด๋ธ”๊ณผ join ํ…Œ์ด๋ธ” ๋ชจ๋‘ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฐ’๋งŒ ๊ฒฐํ•ฉ

    
    -- Inner join
    -- information(A)์™€ score(B)์˜ ๊ต์ง‘ํ•ฉ(key: name)
     select
     	t1.*
     	, t2.subject
     	, t2.score
     from test.information as t1
     inner join test.score as t2 on t1.name = t2.name
    [๊ฒฐ๊ณผ] Inner join
    nonameagesubjectscore
    1Gildong16Korean40
    4Ariel16Math95

    1-4. Full outer join
    ๐Ÿ’ก๊ฒฐํ•ฉํ•˜๋ ค๊ณ  ํ•˜๋Š” ํ…Œ์ด๋ธ”๋“ค์— ์žˆ๋Š” ๊ฐ’ ๋ชจ๋‘ ๊ฒฐํ•ฉ

    
    -- Full outer join
    -- information(A)์™€ score(B)์˜ ํ•ฉ์ง‘ํ•ฉ(key: name)
    select
    	t1.*
    	, t2.subject
    	, t2.score
    from test.information      as t1
    full outer join test.score as t2 on t1.name = t2.name
    [๊ฒฐ๊ณผ] Full outer join
    nonameagesubjectscore
    1Gildong16Korean40
    2Kongjwi17
    3Michael14
    4Ariel16Math95

    1-5. Union & Union all
    ๐Ÿ’ก๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
    ๐Ÿ’กUnion์€ ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ๊ฒฐํ•ฉ, Union all์€ ์ค‘๋ณต ํ—ˆ์šฉ ํ›„ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•
    ๐Ÿ’ก๊ฒฐํ•ฉ ์‹œ, ๋ชจ๋“  ์ฟผ๋ฆฌ๋Š” ์ปฌ๋Ÿผ์˜ ๊ฐฏ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์ผ์น˜ํ•ด์•ผํ•จ

    
    -- Union
    -- information๋ฅผ Union ๊ฒฐํ•ฉ
    select *
    from test.information
    union
    select *
    from test.information
    [๊ฒฐ๊ณผ] Union (์ค‘๋ณต์ œ๊ฑฐ)
    nonameage
    1Gildong16
    2Kongjwi17
    3Michael14
    4Ariel16
    
    -- Union all
    -- information๋ฅผ Union all ๊ฒฐํ•ฉ
    select *
    from test.information
    union all
    select *
    from test.information

    [๊ฒฐ๊ณผ] Union all (์ค‘๋ณตํ—ˆ์šฉ)

    nonameage
    1Gildong16
    2Kongjwi17
    3Michael14
    4Ariel16
    1Gildong16
    2Kongjwi17
    3Michael14
    4Ariel16
profile
Data Analyst

0๊ฐœ์˜ ๋Œ“๊ธ€