데이터베이스

DATABASE STEP 6 - Column

2023. 3. 17. 13:27
728x90

★ 컬럼(Column)

  • 컬럼 명시
select name, buseo
    from tblInsa;
  • 연산
select name, basicpay, basicpay *2 as basicpay2
    from tblInsa;
  • 상수
select name, '홍길동'
    from tblInsa;

 

■ 1. distinct

  • 컬럼 리스트에서 사용
  • 중복값 제거
  • distinct 컬럼명 > distict 컬럼리스트
select * 
    from tblCountry;

-- 14개 국가가 각각 속한 대륙을 가져오시오.
select continent 
    from tblCountry;

-- tblCountry에는 어떤 어떤 대륙이 있나요? > 종류?
select distinct continent 
    from tblCountry;
    
-- tblInsa. > 이 회사에는 어떤 부서들이?
select distinct buseo 
    from tblInsa;
    
-- tblInsa. > 이 회사는 직위가 어떤것들?
select distinct jikwi 
    from tblInsa;

--************* DB의 테이블에는 셀 병합 기능이 없음.  중복값 제거 안됨.
select distinct continent, name 
    from tblCountry;

-- 레코드 전체값을 비교해서 중복값을 제거 함.
select distinct age, height
    from tblAddressBook
        where age = 36;

 

■ 2. case

  • 대부분 절에서 사용
  • 조건문 역할 > 컬럼값 조작
-- gender가 m이면 남자, f면 여자
select 
    last || first as name,
    gender,
    case 
        when gender = 'm' then '남자'
        when gender = 'f' then '여자'
    end as gendername
        from tblComedian;
        
-- Cotinent가 AS면 아시아 ~ SA면 남미
select 
    name,
    continent,
    case
        when continent = 'AS' then '아시아'
        when continent = 'EU' then '유럽'
        when continent = 'AF' then '아프리카'
        when continent = 'SA' then '남미'
         -- else '기타'
         else continent
         -- else capital -- 사용 금지
    end as continentName
from tblCountry;

select
    name,
    continent,
    case continent
        when 'AS' then '아시아'
        when 'EU' then '유럽'
        when 'AF' then '아프리카'
        else '기타'
    end as continentName
from tblCountry;

-- 몸무계 kg 비교 분류
select 
    last || first as name,
    weight,
    case
        when weight > 90 then '과체중'
        when weight > 50 then '정상체중'
        else '저체중'
    end as state,
    case
        when weight >50 and weight <=90 then '정상체중'
        else '이상체중'
    end as state2,
    case
        when weight between 50 and 90 then '정상체중'
        else '이상체중'
    end as state3
from tblComedian;

-- 직위 분류
select
    name, jikwi,
    case
        when jikwi = '부장' or jikwi = '과장' then '간부급'
        else '평사원급'
    end state,
    case 
        when jikwi in ('부장', '과장') then '간부급'
        else '평사원급'
    end state2
from tblInsa;

-- 성별에 따른 점수부여
select 
    name,
    case
        when name like '김%' then 100
        when name like '이%' then 100
        when name like '박%' then 100
        else 50
    end as 가산점
from tblInsa;

-- 미션을 수행했으면 완료 안했으면 미완료
select 
    title,
    case
        when completedate is null then '미완료'
        when completedate is not null then '완료'
    end as state
from tblTodo;

 

■ 종합 요구사항 모음

--요구사항.001.employees
--직업이 어떤것들이 있는지 가져오시오. > job_id
select distinct job_id
from employees

--요구사항.002.employees
--고용일이 2002~2004년 사이인 직원들은 어느 부서에 근무합니까? > hire_date + department_id
select distinct department_id
    from employees
        where hire_date between '2002-01-01' and '2004-12-31'
        
--요구사항.003.employees
--급여가 5000불 이상인 직원들은 담당 매니저가 누구? > manager_id
select distinct manager_id 
    from employees
        where salary >= 5000;
        
--요구사항.004.tblInsa
--남자 직원 + 80년대생들의 직급은 뭡니까? > ssn + jikwi
select distinct jikwi
    from tblInsa
        where ssn like '8%-%'
        
--요구사항.005.tblInsa
--수당 20만원 넘는 직원들은 어디 삽니까? > sudang + city   
select distinct city
    from tblInsa
        where sudang >20;
        
--요구사항.006.tblInsa
--연락처가 아직 없는 직원은 어느 부서입니까? > null + buseo
select distinct buseo
    from tblInsa
        where tel is null;
728x90
저작자표시 비영리 변경금지 (새창열림)

'데이터베이스' 카테고리의 다른 글

DATABASE STEP 8 - Aggregation_Function  (0) 2023.03.17
DATABASE STEP 7 - Order  (0) 2023.03.17
DATABASE STEP 5 - Where  (0) 2023.03.16
DATABASE STEP 4 - Operator  (0) 2023.03.16
DATABASE STEP 3 - select  (0) 2023.03.16
'데이터베이스' 카테고리의 다른 글
  • DATABASE STEP 8 - Aggregation_Function
  • DATABASE STEP 7 - Order
  • DATABASE STEP 5 - Where
  • DATABASE STEP 4 - Operator
IT의 큰손
IT의 큰손
IT계의 큰손이 되고 싶은 개린이의 Log 일지
Developer Story HouseIT계의 큰손이 되고 싶은 개린이의 Log 일지
IT의 큰손
Developer Story House
IT의 큰손
전체
오늘
어제
  • 분류 전체보기 (457)
    • 정보처리기사 필기 (18)
    • 정보처리기사 실기 (12)
    • 정보처리기사 통합 QUIZ (12)
    • 빅데이터 (11)
    • 안드로이드 (11)
    • 웹페이지 (108)
    • 자바 (49)
    • SQLD (3)
    • 백준 알고리즘 (76)
    • 데이터베이스 (41)
    • 깃허브 (2)
    • Library (14)
    • Server (31)
    • 크롤링&스크래핑 (3)
    • Spring (23)
    • Vue.js (13)
    • React (27)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Developer Stroy House

인기 글

태그

  • IT자격증공부
  • jquery
  • it
  • 자바
  • 웹페이지
  • 백엔드
  • React
  • 데이터베이스
  • 코딩테스트
  • 앱개발자
  • IT개발자
  • jsp
  • JavaScript
  • 정보처리기사필기
  • IT자격증
  • java
  • 개발블로그
  • 알고리즘
  • html
  • 정보보안전문가
  • 백준
  • 웹개발
  • css
  • DB
  • 정보처리기사
  • 개발자
  • ajax
  • 프론트엔드
  • DBA
  • 웹개발자

최근 댓글

최근 글

Designed By hELLO
IT의 큰손
DATABASE STEP 6 - Column
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.