★ SQL문

1. 데이타 조작어(DML : Data Manipulation Language)

    : insert, update, delete, merge

2. 데이타 정의어(DDL : Data Definition Language)

    : create, alter, drop, rename, truncate    

3. 데이타검색

    : select 

4. 트랜젝션제어

    : commit, rollback, savepoint

5. 데이타 제어어(DCL : Data Control Language)

    : grant,  revoke



※ select

[형식]

 select [distinct] [컬럼1,컬럼2,.....][as 별명][ || 연산자][*]

 from 테이블명

 [where 조건절] 


distinct : 중복제거

* : 모든

조건절 : and,or,like,in,between and,is null,is not null



ex1) employees테이블의 모든 사원의 사원번호,이름(last_name),급여 검색

select employee_id, last_name, salary from employees;


ex2)별명붙이기(as는 생략가능)

    employees테이블의 모든 사원의 사원번호,이름(last_name),급여 검색

    조건) title 사원번호, 이름 ,급여로 출력할것

select employee_id as 사원번호 , last_name as "이  름", salary as "급  여" 

from employees;



ex3) employee테이블에서 사원번호,이름,연봉을 구하시오

     조건1) 연봉 = 급여 * 12

     조건2) 제목을 사원번호, 이름, 연봉으로 출력

select employee_id as 사원번호 , last_name as "이  름", salary*12 as "연  봉"

from employees;


ex4) 연결연산자( || ) : 컬럼을 연결해서 출력

     frist_name과  last_name을 연결해서 출력하시오

       이   름

     ------------

     Ellen   Abel

select  first_name||'  '||last_name  as  "이  름" from employees;



[문제1] 다음처럼 출력하시오

   사원번호    이  름        연 봉

   -----------------------------------------------------

    100      Steven King   288000달러 


[문제2]다음처럼 출력하시오 (last_name, job_id)

     Employee  Detail

     --------------------

     King  is  a  SA_REP

     King  is  a  AD_PRES



ex5) distinct(중복제거)

     employees 테이블에서 부서ID를 출력하시오

select distinct department_id from employees;


ex6) 10번부서 또는 90번부서 사원들의 이름, 입사일, 부서ID를 출력하시오

select last_name, hire_date, department_id

from employees

where department_id=10  or  department_id=90;


ex7) 급여가 2500이상 3500미만인 사원의 이름(last), 입사일, 급여를 검색하시오

select last_name, hire_date, salary

from employees

where salary>=2500  and  salary<3500;



[문제3] 급여가 2500이하 이거나 3000이상이면서 90번 부서인 

사원의 이름, 급여, 부서ID를 출력하시오.

조건1) 제목은 사원명, 월  급, 부서코드로 하시오

조건2) 급여앞에 $를 붙이시오

조건3) 사원명은 first_name과 last_name을 연결해서 출력하시오

ex8) 'King'사원의 모든 컬럼을 표시하시오

select  *  from employees  where  last_name='King';

→ 문자열 검색할 때는 대,소문자를 구분


like  : 문자를 포함

'%d' - d로 끝나는

      'a%' - a로 시작하는

      '%test% - test가 포함되어있는


      '_a%'  - 두번째 글자가 a로 시작하고 나머지는 무시

      '__a%' - 세번째 글자가 a로 시작하고 나머지는 무시


ex9) 업무ID에 MAN이 포함되어있는 사원들의 이름,업무ID,부서ID를 출력하시오

select last_name, job_id, department_id

from employees

where job_id like '%MAN%';


ex10) 업무ID가  IT로 시작하는 사원들의 이름, 업무ID, 부서ID를 출력하시오

select last_name, job_id, department_id

from employees

where job_id like 'IT%';


ex11) is null / is not null 

커미션을 받는 사원들의 이름과 급여, 커미션을 출력하시오

select last_name, salary, commission_pct

from employees

where commission_pct is not null;

커미션을 받지 않는 사원들의 이름과 급여, 커미션을 출력하시오

select last_name, salary, commission_pct

from employees

where commission_pct is null;


ex12) in연산자(or연산자의 다른표현)

업무ID가  FI_MGR이거나  FI_ACCOUNT인 사원들의 사원번호,이름,직무를 출력하시오

select employee_id, last_name, job_id

from employees

where job_id='FI_MGR' or job_id='FI_ACCOUNT';



select employee_id, last_name, job_id

from employees

where job_id in('FI_MGR', 'FI_ACCOUNT');


ex13) between연산자(and연산자의 다른 표현) : 초과,미만에서는 사용할수 없다

급여가  10000이상  20000이하인 사원의 사원번호,이름,급여를 출력하시오

select employee_id, last_name, salary

from employees

where salary>=10000 and salary<=20000;


select employee_id, last_name, salary

from employees

where salary between 10000 and 20000;



[문제4] 업무ID가 'SA_REP' 이거나 'AD_PRES' 이면서 급여가 10000를 

초과하는 사원들의 이름,업무ID,급여를 출력하시오


이름        업무ID            급  여

--------------------------------------

King       AD_PRES            24000원



[문제5] Employees테이블의 업무ID가 중복되지 않게 표시하는 질의를 작성하시오


  JOB_ID

-----------

AC_ACCOUNT

AC_MGR

     :



[문제6] 입사일이 2005년인 사원들의 사원번호, 이름, 입사일을 표시하시오



+ Recent posts