본문 바로가기

IT Programmer/Java (자바 과정)

[ 3.SQL 활용 ] DML_Select


-- Result Set : SELECT구문으로 데이터를 조회한 결과물, 반환된 행의 집합을 의미한다,
desc employee;
select *from employee;
-- 특정 컬럼들 검색하기
select emp_id, emp_name, salary from employee;

-- job테이블의 모든 정보 조회하기
select *from job;

--job 테이블의 job_name 조회하기
select job_name from job;

-- department 테이블의 모든 정보 조회하기
select *from department;

-- employee 테이블의 직원명, 이메일, 전화번호, 고용일 조회
select*from employee;
select emp_name, email, phone, hire_date from employee;

-- employee 테이블의 고용일, 사원 이름, 월급 조회하기.
select*from employee;
select hire_date, emp_name, salary from employee;

-- 컬럼 값 산술연산
-- 산술연산자 : + - * / 
    select사 컬럼명 입력 부분에 계산에 필요한 컬럼명, 숫자, 연산자를 이용하여, 결과를 조회할 수 있다.

-- employee 테이블에서 직원의 직원명, 연봉 조회 (연봉은 급여*12)
select emp_name, salary*12 from employee;

-- employee 테이블에서 직원의 직원명, 연봉, 보너스를 추가한 연봉을 조회
select emp_name, salary*12, (salary + (salary+bonus))*12 from employee;

 

-- null 표시 수정 + 별칭 넣기 [ 별칭 붙일 때, 특수기호, 숫자있으면 무조건 " " 사용해야함 ] 
select emp_name, salary*12  as "총수령액", (salary + (salary+NVL(bonus,0)))*12 as "보너스포함금액" from employee;


-- 리터럴
   임의로 지정한 문자열을 select절에서 사용하면 테이블에 존재하는 데이터 처럼 사용할 수 있다.
   리터럴은 Result Set의 모든 행에 반목 표시된다.

-- Employee 테이블에서 직원의 직원번호, 사원명, 급여, 단위('원') 조회
select emp_id, emp_name, salary, '원' as "단위" from employee;

-- 연결 연산자
   여러칼럼을 하나의 칼럼인 것 처럼 연결해서 추출하거나 컬럼과 문자열을 연결할 수 있다.
select emp_id || emp_name || salary from employee;


-- 컬럼과 리터럴(문자열) 연결
select emp_name || '의 월급은 ' || salary || '원 입니다.' from employee;

-- 실습문제
-- 1. Employee 테이블에서 이름, 연봉, 총수령액(보너스 포함), 실수령액(총수령액 - (연봉*세금 3%)) 조회
select emp_name, (salary*12), (salary+(salary*NVL(bonus,0)))*12 as "총 수령액"
, (salary+(salary*NVL(bonus,0)))*12 - (salary*12*0.03) as "실 수령액" from employee;

-- 2. employee 테이블에서 이름, 고용일, 근무일수(오늘 날짜 - 고용일) 조회
select emp_name, hire_date, round(sysdate - hire_date,0) as " 근무일수 " from employee;

-- where절 :  조회할 테이블에서 조건에 맞는 값을 가진 행을 골라냄

-- 비교 연산자  = >=  < <= ( != , ^=, <> )

--  employee 테이블에서 부서코드가 '09'인 직원의 이름, 부서코드 조회
select emp_name, dept_code from employee where dept_code = 'D9' ;

-- employee 테이블에서 급여가 4000000 이상인 직원의 이름, 급여 조회
select emp_name, salary from employee where salary >=4000000;

-- employee 테이블에서 부서코드가 'D9'가 아닌 직원의 이름, 부서코드 조회,
select emp_name, dept_code from employee where dept_code != 'D9' ;

-- employee 테이블에서 급여를 350만원 이상 600만원 이하 받는 직원의 사번, 이름, 급여 부서코드를 조회
select emp_name, dept_code, salary from employee where salary>=3500000 and salary<=6000000;

-- null 논리연산 확인
    null or true = true;
    null and false = false;
    나머지의 경우는 null

-- null or true = true는 true이다. 결과 값 출력됨.
select*from employee where bonus = null or emp_id = 201;


-- null and false = false는 false 다
select*from employee where not( bonus = null and emp_id = 201 );


-- between and
   비교 컬럼명 between A and B : A이상 B이하

-- 급여를 3500000원 보다 많이 받고 6000000원보다 적게 받는 사원 이름, 급여 조회
-- 원래 코드 ) select emp_name, dept_code, salary from employee where salary>=3500000 and salary<=6000000;
select emp_name, dept_code, salary from employee where salary between 3500000 and 6000000;


-- 실습
-- 1. EMPLOYEE 테이블에서 월급이 3000000이상인 사원의 이름, 월급, 고용일 조회
select  emp_name, salary, hire_date from employee where salary >=3000000;

-- 2. EMPLOYEE 테이블에서 SAL_LEVEL이 S1인 사원의 이름, 월급, 고용일, 연락처 조회
select emp_name, salary, hire_date, phone from employee where sal_level = 'S1';

-- 3. EMPLOYEE 테이블에서 실수령액(총수령액 - (연봉*세금 3%))이 5천만원 이상인 사원의 이름, 월급, 실수령액, 고용일 조회
select  emp_name, salary, (salary+(salary*NVL(bonus,0)))*12 - (salary*12*0.03) as "실수령액" , hire_date from employee where  round(salary*12 - (salary*12*0.3),0) >= 50000000;

-- 4. EMPLOYEE 테이블에 월급이 4000000이상이고 JOB_CODE가 J2인 사원의 전체 내용 조회
select*from employee where salary >= 4000000 and job_code = 'J2';

-- 5. EMPLOYEE 테이블에 DEPT_CODE가 D9이거나 D5인 사원 중 고용일이 02년 1월 1일보다 빠른 사원의 이름, 부서코드, 고용일 조회
      ( 다중 조건일 경우 순서 중요 먼저 조건을 검색해야되는 경우 괄호를 묶을 것 )
select emp_name, dept_code, hire_date from employee where (dept_code = 'D9' or dept_code='D5') and hire_date <= '02/01/01 ';

-- 6. EMPLOYEE 테이블에 고용일이 90/01/01 ~ 01/01/01인 사원의 전체 내용을 조회
select*from employee where hire_date between '90/01/01' and '01/01/01';


-- Like 
   비교하려는 값이 지정한 특정 패턴을 만족시키는지 조회할 때 사용
   비교대상 컬럼명 LIKE '문자패턴'
%  : 글자% (글자로 시작하는 값) %글자(글자로 끝나는 값) %글자%(글자가 포함된 값)
 _  : 문자수 : '_'(한 글자), '__'(두 글자), '___'(세 글자)

-- Employee 테이블에서 '하'가 포함된 직원의 이름, 주민번호, 부서코드 조회
select emp_name, emp_no, dept_code from employee where emp_name like'%하%';

-- Employee 테이블에서 전화번호 4번째 자리가 9로 시작하는 사원의 사번, 이름 , 전화번호 조회
select emp_id, emp_name, phone from employee where phone like '___9%';

-- escape 
   와일드카드와 패턴의 특수문자가 동일한 경우
   어떤 것이 패턴이고 어떤 것이 특수문자인지 구분할 수가 없다.
   이 경우 데이터로 처리할 특수문자 앞에 임의의 특수문자를 사용하고 escape 옵션으로 등록해준다.

-- 이메일 중 _앞글자가 3자리인 이메일 주소를 가진 사원의 사번, 이름, 이메일주소 조회
select emp_id, emp_name, email from employee where email like '___!_%' escape '!' ;

-- not Like
-- employee 테이블에서 김씨 성이 아닌 직원의 사번, 이름, 고용일 조회
select emp_id, emp_name, hire_date from employee where emp_name not like'김%';

-- 실습문제
-- employee 테이블에서 이름이 '연'으로 끝나는 사원의 이름 조회
select emp_name from employee where emp_name like '%연';

-- employee 테이블에서 전화번호 처음 3자리가 010인 아닌 사원의 이름, 전화번호를 조회
select emp_name, phone from employee where phone not like '010%';

-- employee 테이블에서 메일주소 '_' 의 앞의 4자리 이면서, dept_code가 D9 또는 D6이고, 고용일이 90/01/01 ~ 00/12/01이고, 급여가 270만 이상인 사원의 전체를 조회
select*from employee 
where email like '____!_%' escape '!' 
and (dept_code = 'D9' or  dept_code = 'D6' ) 
and hire_date between '90/01/01' and '00/12/01' 
and salary >= 2700000;

-- is null / is not null
   is null : 컬럼값이 null경우에 true
   is not null : 컬럼값이 null이 아닌 경우에  true

-- 보너스를 받지 않는 사원의 정보를 출력하시오.
select *from employee where bonus is null;
-- 관리자도 없고 부서 배치도 받지않는 직원의 정보를 출력하시오
select*from employee where Manager_id is null and dept_code is null;

-- 부서배치를 받지 않았지만 보너스를 지급받은 직원의 정보를 출력하시오.
select*from employee where dept_code is null and bonus is not null;

-- in
   비교하려는 값 목록에 일치하는 값이 있으면 true
select*from employee where dept_code in ('06','09');

-- 연산자 우선순위
산술연산자  -> 연결연산자 -> 비교연산자 -> IS NULL / IS NOT NULL / LIKE / IN / NOT IN ->

        BETWEEN AND / NOT BETWEEN AND -> NOT(논리연산자) -> AND(논리연산자) -> OR(논리연산자)

-- distinct
   컬럼에 포함된 중복값을 한번씩만 표시하고자 할 때 사용


-- employee 테이블에서 직원의 직급코드 조회
select distinct job_code from employee;

-- order by
   select한 컬럼에 대해 정렬을 할 때 작성하는 구문
   select문의 마지막에 작성, 실행순서도 마지막
   실행순서 : from -> where -> group by -> having ->  select -> order by
   order by 속성명 asc(오름차순) desc(내림차순)
   null first(null값이 있는 데이터를 위로),
   null last(null값이 있는 데이터를 아래로)

select *from employee order by job_code asc; --오름차순
select *from employee order by job_code desc; -- 내림차순


-- job_code는 오름차순을 하면서 dept_code는 내림차순일때
select*from employee order by job_code asc, dept_code desc;