๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/Oracle

[ex04~07] operator, where, order by

ex04_operator

   ์—ฐ์‚ฐ์ž, Operator
    
    1. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
    - +, -, *, /
    - %(์—†์Œ) > ํ•จ์ˆ˜๋กœ ์ œ๊ณต(mod())
    
    2. ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ž
    - ||(java์—์„œ + ์—ญํ• )
    
    3. ๋น„๊ต ์—ฐ์‚ฐ์ž
    - >, >=, <, <=
    - =(==), <>(!=)
    - ๋…ผ๋ฆฌ๊ฐ’ ๋ฐ˜ํ™˜ X > ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜• > ์กฐ๊ฑด์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์—์„œ๋งŒ ์‚ฌ์šฉ
    - ์ปฌ๋Ÿผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
    - ์กฐ๊ฑด์ ˆ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    
    4. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
    - and(&&), or(||), not(!)
    - ์ปฌ๋Ÿผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
    - ์กฐ๊ฑด์ ˆ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    
    5. ๋Œ€์ž… ์—ฐ์‚ฐ์ž
    - =
    - ์ปฌ๋Ÿผ = ๊ฐ’
    - update๋ฌธ
    
    8. SQL ์—ฐ์‚ฐ์ž
    - ์ž๋ฐ”: instanceof, typeof ๋“ฑ..
    - in, between, like, is ๋“ฑ..(OO๊ตฌ, OO์ ˆ)

 

(3ํ•ญ์—ฐ์‚ฐ์ž, ์ฆ๊ฐ์—ฐ์‚ฐ์ž ์—†์Œ)

 

desc tblCountry;
select * from tblCountry;


select population, area, population + area 
    from tblCountry;

-- ORA-01722: invalid number
select name + capital
    from tblCountry;

select name || capital
    from tblCountry;

-- ์ปฌ๋Ÿผ๋ช… > ๊ฐ€๊ณต๋œ ์ปฌ๋Ÿผ๋ช… > ์˜ฌ๋ฐ”๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ˆ˜์ • > ์ปฌ๋Ÿผ๋ช… ๋ฐ”๊พธ๊ธฐ > ๋ณ„์นญ(Alias)
-- ์‹๋ณ„์ž > "name || '๋‹˜'"
select name, name || '๋‹˜'
    from tblInsa;

----์‹๋ณ„์ž์— ๊ณต๋ฐฑ ์—†๋Š”๊ฒŒ ์ข‹์Œ.. ๊ณต๋ฐฑ ๋ถˆ๊ฐ€ํ”ผํ•˜๊ฒŒ ํฌํ•จํ• ๋•Œ or ์˜ˆ์•ฝ์–ด ์Œ๋”ฐ์˜ดํ‘œ(๋กœ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์‚ฌ์šฉ ๊ธˆ์ง€)

ex05_where

    select ์ปฌ๋Ÿผ๋ฆฌ์ŠคํŠธ  --3. ์ปฌ๋Ÿผ ์ง€์ •
    from ํ…Œ์ด๋ธ”           --1. ํ…Œ์ด๋ธ” ์ง€์ •
    where ์กฐ๊ฑด;           --2. ์กฐ๊ฑด ์ง€์ •
    
    
    where์ ˆ
    - ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
    - ์›ํ•˜๋Š” ํ–‰๋งŒ ์ถ”์ถœํ•˜๋Š” ์—ญํ•  > ๊ฒฐ๊ณผ์…‹ ๋ฐ˜ํ™˜

 

select name --3. 
    from tblCountry --1.
        where continent = 'AS'; --2.

select *
    from tblInsa
        where basicpay > 2000000;
        
select *
    from tblInsa
        where buseo <> '๊ฐœ๋ฐœ๋ถ€';

select *
    from tblInsa
        where buseo = '๊ฐœ๋ฐœ๋ถ€' and jikwi = '๋ถ€์žฅ';
        
select *
    from tblInsa
        where not buseo = '๊ฐœ๋ฐœ๋ถ€';

 

-- SQL ๊ตฌ๋ฌธ > ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์•ˆํ•จ
-- ๋ฐ์ดํ„ฐ > ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•จ(***)

 

 

 

    between
    - where์ ˆ์—์„œ ์‚ฌ์šฉ > ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ
    - ์ปฌ๋Ÿผ๋ช… between ์ตœ์†Ÿ๊ฐ’ and ์ตœ๋Œ“๊ฐ’ (๋‘˜ ๋‹ค ํฌํ•จ)
    - ๋ฒ”์œ„ ์กฐ๊ฑด
    - ๊ฐ€๋…์„ฑ(***)

select * from tblComedian
    where height >= 172 and height <= 178;

select * from tblComedian
    where height between 172 and 178;
    
-- ๋น„๊ต์—ฐ์‚ฐ
-- 1. ์ˆซ์žํ˜•
select * from tblInsa where basicpay >= 1500000 and basicpay <= 2000000;
select * from tblInsa where basicpay between 1500000 and 2000000;

-- 2. ๋ฌธ์žํ˜•
select * from tblInsa where name >= '๋ฐ•';
select * from tblInsa where name >= '๋ฐ•' and name <= '์œ ';
select * from tblInsa where name between '๋ฐ•' and '์œ ';

-- 3. ๋‚ ์งœ์‹œ๊ฐ„ํ˜•
select * from tblInsa where ibsadate >= '2000-01-01'; --2000๋…„ ์ดํ›„์— ์ž…์‚ฌํ•œ ์ง์›๋“ค
select * from tblInsa where ibsadate >= '2000-01-01' and ibsadate <= '2000-12-31';
select * from tblInsa where ibsadate between '2000-01-01' and '2000-12-31';

 

    in
    - where์ ˆ์—์„œ ์‚ฌ์šฉ > ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ
    - ์—ด๊ฑฐํ˜• ์กฐ๊ฑด
    - ์ปฌ๋Ÿผ๋ช… in (๊ฐ’, ๊ฐ’, ๊ฐ’..)
    - ๊ฐ€๋…์„ฑ

select * from tblInsa where buseo = 'ํ™๋ณด๋ถ€' or buseo = '๊ฐœ๋ฐœ๋ถ€' or buseo = '์ด๋ฌด๋ถ€';
select * from tblInsa where buseo in ('ํ™๋ณด๋ถ€', '๊ฐœ๋ฐœ๋ถ€', '์ด๋ฌด๋ถ€');

select * from tblInsa
    where jikwi in ('๊ณผ์žฅ', '๋ถ€์žฅ') and city in ('์„œ์šธ', '์ธ์ฒœ')
        and basicpay between 2500000 and 3000000;

 

   like
    - where์ ˆ์—์„œ ์‚ฌ์šฉ > ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ
    - ํŒจํ„ด ๋น„๊ต
    - ์ปฌ๋Ÿผ๋ช… like 'ํŒจํ„ด ๋ฌธ์ž์—ด'
    - ์ •๊ทœ ํ‘œํ˜„์‹ ์ดˆ๊ฐ„๋‹จ ๋ฒ„์ „
    
    ํŒจํ„ด ๋ฌธ์ž์—ด ๊ตฌ์„ฑ์š”์†Œ
    1. _: ์ž„์˜์˜ ๋ฌธ์ž 1๊ฐœ (.)
    2. %: ์ž„์˜์˜ ๋ฌธ์ž N๊ฐœ 0~๋ฌดํ•œ๋Œ€ (.*)

-- ๊น€OO
select name from tblInsa where name like '๊น€__';
select name from tblInsa where name like '__์ˆ˜';
select name from tblInsa where name like '_๊ธธ_';

select name from tblInsa where name like '๊น€%';

select * from tblAddressBook where name like '์ด%';
select * from tblAddressBook where name like '%์ด';
select * from tblAddressBook where name like '%์ด%';

-- ์—ฌ์ง์›๋งŒ(์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ์‚ฌ์šฉ)
select * from tblInsa where ssn like '______-2______';
select * from tblInsa where ssn like '%-2%';

 

    null ์กฐ๊ฑด
    - where์ ˆ์—์„œ ์‚ฌ์šฉ
    - ์ปฌ๋Ÿผ๋ช… is null

    - null์€ ์—ฐ์‚ฐ์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์—†๋‹ค.

-- ์ธ๊ตฌ์ˆ˜๊ฐ€ ๋ฏธ๊ธฐ์žฌ๋œ ๋‚˜๋ผ?
select * from tblCountry where population = null; -- ํ‹€๋ฆฐ ๋ฌธ์žฅ
select * from tblCountry where population is null;
select * from tblCountry where not population is null; -- null์ด ์•„๋‹Œ ๊ฒฝ์šฐ
select * from tblCountry where population is not null; -- null์ด ์•„๋‹Œ ๊ฒฝ์šฐ(*์ถ”์ฒœ)

-- ์•„์ง ์‹คํ–‰ํ•˜์ง€ ์•Š์€ ์ผ?
select * from tblTodo where completedate is null;
--์™„๋ฃŒํ•œ ์ผ?
select * from tblTodo where completedate is not null;

-- ๋„์„œ๊ด€ > ๋Œ€์—ฌ ํ…Œ์ด๋ธ”(์†์„ฑ: ๋Œ€์—ฌ๋‚ ์งœ, ๋ฐ˜๋‚ฉ๋‚ ์งœ)
-- ์•„์ง ๋ฐ˜๋‚ฉ์„ ์•ˆํ•œ ์‚ฌ๋žŒ์€? 
select * from ๋„์„œ๋Œ€์—ฌ where ๋ฐ˜๋‚ฉ๋‚ ์งœ is null;
-- ๋ฐ˜๋‚ฉ์ด ์™„๋ฃŒ๋œ ์‚ฌ๋žŒ์€?
select * from ๋„์„œ๋Œ€์—ฌ where ๋ฐ˜๋‚ฉ๋‚ ์งœ is not null;

ex06_column

์ปฌ๋Ÿผ ๋ฆฌ์ŠคํŠธ์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ํ–‰๋™
    - select ์ปฌ๋Ÿผ๋ฆฌ์ŠคํŠธ

--์ปฌ๋Ÿผ ๋ช…์‹œ
select name, buseo
    from tblInsa;

-- ์—ฐ์‚ฐ
select name, basicpay, basicpay * 2 as basicpay2
    from tblInsa;

-- ์ƒ์ˆ˜(๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์— ๋™์ผํ•œ ๋ฐ์ดํ„ฐ)
select name, 'ํ™๊ธธ๋™', 100
    from tblInsa;

 

distinct
    - ์ปฌ๋Ÿผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ
    - ์ค‘๋ณต๊ฐ’ ์ œ๊ฑฐ
    - distinct ์ปฌ๋Ÿผ๋ช… > distinct ์ปฌ๋Ÿผ๋ฆฌ์ŠคํŠธ

 

-- 14๊ฐœ ๊ตญ๊ฐ€๊ฐ€ ๊ฐ๊ฐ ์†ํ•œ ๋Œ€๋ฅ™์„ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.
select continent from tblCountry;

-- tblCountry์—๋Š” ์–ด๋–ค ์–ด๋–ค ๋Œ€๋ฅ™์ด ์žˆ๋‚˜์š”? > ์ข…๋ฅ˜?
select distinct continent from tblCountry;

-- tblInsa > ์ด ํšŒ์‚ฌ์—๋Š” ์–ด๋–ค ๋ถ€์„œ๋“ค์ด?
select distinct buseo from tblInsa;

-- tblInsa > ์ด ํšŒ์‚ฌ์—๋Š” ์–ด๋–ค ์ง์œ„๊ฐ€?
select distinct jikwi from tblInsa;


-- ************ DB์˜ ํ…Œ์ด๋ธ”์—๋Š” ์…€๋ณ‘ํ•ฉ์ด๋ผ๋Š”๊ฒŒ ์—†๋‹ค.
-- distinct ๋’ค์— ์ปฌ๋Ÿผ์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ ๋ชจ๋‘ ์ค‘๋ณต๋˜์–ด์•ผ ์ค‘๋ณต๊ฐ’์ฒ˜๋ฆฌ
select distinct continent, name from tblCountry;

select distinct age, height from tblAddressBook where age = 36; --53 > 29

 

    case
    - ๋Œ€๋ถ€๋ถ„ ์ ˆ์—์„œ ์‚ฌ์šฉ
    - ์กฐ๊ฑด๋ฌธ ์—ญํ•  > ์ปฌ๋Ÿผ๊ฐ’ ์กฐ์ž‘

 

select 
    last || first as name,
    gender,
    case
        when gender = 'm' then '๋‚จ์ž'
        when gender = 'f' then '์—ฌ์ž'
    end as genderName
from tblComedian;

select 
    name,
    continent,
    case continent
        when 'AS' then '์•„์‹œ์•„'
        when 'EU' then '์œ ๋Ÿฝ'
        when 'AF' then '์•„ํ”„๋ฆฌ์นด'
    end as continentName
from tblCountry;

select
    name, jikwi,
    case
        when jikwi = '๋ถ€์žฅ' or jikwi = '๊ณผ์žฅ' then '๊ฐ„๋ถ€๊ธ‰'
        else 'ํ‰์‚ฌ์›๊ธ‰'
    end state,
    case
        when jikwi in ('๋ถ€์žฅ', '๊ณผ์žฅ') then '๊ฐ„๋ถ€๊ธ‰'
        else 'ํ‰์‚ฌ์›๊ธ‰'
    end state2
from tblInsa;

select 
    title,
    case
        when completedate is null then '๋ฏธ์™„๋ฃŒ'
        when completedate is not null then '์™„๋ฃŒ'
    end as state
from tblTodo;

ex07_order

 

    select ์ปฌ๋Ÿผ๋ฆฌ์ŠคํŠธ      --3. ์›ํ•˜๋Š” ์ปฌ๋Ÿผ๋“ค์„
    from ํ…Œ์ด๋ธ”            --1. ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ
    where ์กฐ๊ฑด             --2. ์›ํ•˜๋Š” ํ–‰๋“ค์„
    order by ์ •๋ ฌ๊ธฐ์ค€;     --4. ์ˆœ์„œ๋Œ€๋กœ
    
    order by ์ ˆ
    - ๊ฒฐ๊ณผ์…‹์˜ ์ •๋ ฌ(O)
    - ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ์ •๋ ฌ(์‚ฌ์šฉ์ž๊ฐ€ ๊ด€์—ฌ ๋ถˆ๊ฐ€๋Šฅ > ์˜ค๋ผํด ์Šค์Šค๋กœ)
    - order by ์ •๋ ฌ์ปฌ๋Ÿผ [asc|desc] ์ƒ๋žต์‹œ asc(์˜ค๋ฆ„์ฐจ์ˆœ)

 

select * from tblCountry order by name asc;
select * from tblCountry order by population desc; -- null ์ปฌ๋Ÿผ์„ ๋Œ€์ƒ์œผ๋กœ ์ •๋ ฌ
select * from tblCountry where population is not null order by population desc;


select * from tblInsa order by name asc; --๋ฌธ์ž์—ด + ์˜ค๋ฆ„์ฐจ์ˆœ
select * from tblInsa order by basicpay; --์ˆซ์ž + ์˜ค๋ฆ„์ฐจ์ˆœ 
select * from tblInsa order by ibsadate; --๋‚ ์งœ + ์˜ค๋ฆ„์ฐจ์ˆœ

-- ๋‹ค์ค‘ ์ •๋ ฌ
select * from tblInsa order by buseo asc, city asc, name asc; 

select
    name, buseo, jikwi
from tblInsa
    order by buseo, jikwi, name;
    
select
    name, buseo, jikwi
from tblInsa
    order by 2, 3, 1; --๋น„๊ถŒ์žฅ > ๊ฐ€๋…์„ฑ ๋‚ฎ์Œ, ์œ ์ง€๋ณด์ˆ˜์— ์ทจ์•ฝ

 

-์ง์œ„์ˆœ์œผ๋กœ ์ •๋ ฌ: ๋ถ€์žฅ > ๊ณผ์žฅ > ๋Œ€๋ฆฌ > ์‚ฌ์›

- ์„ฑ๋ณ„์ˆœ์œผ๋กœ ์ •๋ ฌ

select
    name, jikwi,
    case
        when jikwi = '๋ถ€์žฅ' then 1
        when jikwi = '๊ณผ์žฅ' then 2
        when jikwi = '๋Œ€๋ฆฌ' then 3
        when jikwi = '์‚ฌ์›' then 4
    end as jikwiSeq
from tblInsa
    order by jikwiSeq asc;


select
    name, jikwi,
    case
        when jikwi = '๋ถ€์žฅ' then 1
        when jikwi = '๊ณผ์žฅ' then 2
        when jikwi = '๋Œ€๋ฆฌ' then 3
        when jikwi = '์‚ฌ์›' then 4
    end
from tblInsa
    order by 3 asc; --์ธ๋ฑ์Šค์ฒ˜๋ฆฌ๋„ ๊ฐ€๋Šฅ 

-- case ํ–‰ ์•ˆ๋ณด์ด๊ฒŒ > order by ๋‹ค์Œ case
select
    name, jikwi
from tblInsa
    order by case
        when jikwi = '๋ถ€์žฅ' then 1
        when jikwi = '๊ณผ์žฅ' then 2
        when jikwi = '๋Œ€๋ฆฌ' then 3
        when jikwi = '์‚ฌ์›' then 4
    end asc;


-- where์ ˆ์— case ๋„ฃ์„์ˆ˜ ์žˆ์Œ
select
    name, jikwi
from tblInsa
    where case
        when jikwi = '๋ถ€์žฅ' then 1
        when jikwi = '๊ณผ์žฅ' then 2
        when jikwi = '๋Œ€๋ฆฌ' then 3
        when jikwi = '์‚ฌ์›' then 4
    end = 1
    order by case
        when jikwi = '๋ถ€์žฅ' then 1
        when jikwi = '๊ณผ์žฅ' then 2
        when jikwi = '๋Œ€๋ฆฌ' then 3
        when jikwi = '์‚ฌ์›' then 4
    end asc;
   
   
-- ์„ฑ๋ณ„์ˆœ์œผ๋กœ ์ •๋ ฌ: ๋‚จ์ž > ์—ฌ์ž
-- 771212-1022432
select * from tblInsa;

select * from tblInsa
    order by case
                when ssn like '%-1%' then '๋‚จ์ž'
                when ssn like '%-2%' then '์—ฌ์ž'
            end asc;