티스토리 뷰

Database ORACLE

1123

푸른하늘댁 2017. 12. 5. 16:08

테이블구조확인: describe(desc) 테이블명

describe employees

desc employees


조회: select문

select 절 - 컬럼명목록, 모든컬럼(*) 

from 절 - table명

where 절; 조건절



부서코드가 80인 부서에 속한 사원들의 

사번, 성, 명, 급여, 부서코드 조회

--select employee_id, last_name, first_name, salary, department_id

select * 

from employees

where department_id=80;


연산자

산술연산자: 조건절, select 절 사용 가능

비교연산자: >, <, >=, <=, =, !=, <>


년봉이 120000 이상인 사원들의 

사번, 성, 급여, 년봉 조회

select employee_id, last_name, salary, salary*12

from employees 

where salary*12 >= 120000;


날짜/문자 데이터표현: ''

2006년 이후에 입사한 사원들의

사번, 성, 업무코드, 입사일자 조회

select employee_id, last_name, job_id, hire_date

from employees

where hire_date >= '2006-01-01';


조건연산자: 모든 조건을 만족하는 결과: and 

           만족하는 조건에 해당하는 결과: or

부서코드가 80번 이상에 속하고 급여가 10000 이상인 사원들의 

사번, 성, 부서코드, 급여 조회           

select employee_id, last_name, department_id, salary

from employees

where department_id>=80 and salary>=10000;


부서코드가 40번 이하에 속했거나 급여가 10000 이상인 사원들의 

사번, 성, 부서코드, 급여 조회           

select employee_id, last_name, department_id, salary

from employees

where department_id<=40 or salary>=10000;


문자열 연결연산자: ||

사번, 성명 조회

select employee_id as id, first_name ||'  '|| last_name name

from employees;


컬럼표현에 연산자가 사용되거나 컬럼명 지나치게 길때

컬럼표현을 alias 를 두어 사용한다.

컬럼표현 ( as ) alias명 


범위연산자: 컬럼표현 between A and B

       <-> 컬럼표현 not between A and B,

           not 컬럼표현 between A and B 

급여가 10000 이상 12000 이하에 해당하는 사원들의

사번, 성, 업무코드, 급여 조회

select employee_id, last_name, job_id, salary

from employees

where salary>= 10000 and salary<=12000 ;

-> 

select employee_id, last_name, job_id, salary

from employees

where salary between 10000 and 12000;


급여가 10000 미만 12000 초과에 해당하는 사원들의

사번, 성, 업무코드, 급여 조회

select employee_id, last_name, job_id, salary

from employees

where salary< 10000 or salary>12000 ;

->

select employee_id, last_name, job_id, salary

from employees

where not salary between 10000 and 12000;

--where salary not between 10000 and 12000;


사번이 100 이상 110 이하에 해당하는 사원의 

사번, 성 조회

select employee_id, last_name

from employees

where employee_id between 100  and 110;


사번이 100, 101, 102, 103, 104, 105, 106 해당하는 사원의

사번, 성 조회

select employee_id, last_name

from employees

where employee_id = 100 or employee_id = 101 or

 employee_id = 102 or employee_id = 103  ;

동일한 표현에 대해 or 연산자로 나열되어 있는 형태의 조건식

in 연산자 를 사용한다. - 컬럼표현 in ( 데이터값목록 )

                   <-> 컬럼표현 not in ( 데이터값목록 )

                       not 컬럼표현 in ( 데이터값목록 )


select employee_id, last_name

from employees

where employee_id in (100,101,102,103,104,105);

 

사번이 100, 101,102, 103 가 아닌 사원들의 

사번, 성 조회

select employee_id, last_name

from employees

where employee_id != 100 and employee_id != 101 and

 employee_id <> 102 and employee_id <> 103  ; 

 

select employee_id, last_name

from employees

where not employee_id in (100,101,102,103,104,105);

where employee_id not in (100,101,102,103,104,105);


7.like 연산자: %와 함께 사용한다. _와 함께 사용할 수도 있다.

% : 어떤 문자가 오던지, 몇개의 문자가 오던지 무관한

_ : 딱 한개의 어떤 문자가 오던지 무관한 

컬럼표현 like '%문자' : 문자로 끝나는

컬럼표현 like '문자%' : 문자로 시작하는

컬럼표현 like '%문자%' : 문자가 포함된


성이 S로 시작하는 성을 갖는 사원들의

사번, 성 조회

select employee_id, last_name

from employees

where last_name like 'S%';


성이 s로 끝나는 성을 갖는 사원들의

사번, 성 조회

select employee_id, last_name

from employees

where last_name like '%s';


성에 대소문자 무관하게 s/S가 포함된 성을 갖는 사원들의

사번, 성 조회

select employee_id, last_name

from employees

where last_name like '%s%' or last_name like '%S%';


우리회사 사원들의

사번, 성, 업무코드 조회

select employee_id, last_name, job_id

from employees;


성에 s가 세번째 위치에 있는 성을 갖는 사원들의

사번, 성 조회

select employee_id, last_name

from employees

where last_name like '__s%';


성에 s가 뒤에서 세번째 위치에 있는 성을 갖는 사원들의

사번, 성 조회

select employee_id, last_name

from employees

where last_name like '%s__';


업무코드에 _A 가 포함된 업무를 하는 사원들의

사번, 성, 업무코드 조회: FI_ACCOUNT, AD_ASST, AC_ACCOUNT

select employee_id, last_name, job_id

from employees

where job_id like '%?_A%' escape '?';


like 연산자와 함께 사용된 _ , % 를 문자 자체로 인지하게 해야 한다.

_, %앞에 기호문자를 붙이고 

해당 기호문자를 제외시키는 escape 옵션을 사용한다.


우리회사 사원들의

사번, 성, 부서코드 조회

select employee_id, last_name, department_id

from employees;


데이터값이 없는 것: null

- 비교불가, 연산불가

is null : null인(값이 없는) <-> is not null: null이 아닌(값이 있는)


우리회사 사원들중 부서배치 받지 않은 사원의

사번, 성, 부서코드 조회

select employee_id, last_name, department_id

from employees

where department_id is not null;


80번 부서원들의 

사번, 성, 급여, 업무코드 조회, 급여가 낮은 사원부터 높은 사원 순으로 정렬

select employee_id, last_name, salary, job_id

from employees

where department_id=80

order by salary asc;


사번, 성, 급여, 업무코드 조회, 급여가 높은 사원부터 낮은 사원 순으로 정렬

select employee_id, last_name, salary, job_id

from employees

where department_id=80

order by salary desc;


데이터행 정렬: order by 절 + 기준1 + 순서, 기준2 + 순서

             (오름차순: asc default/내림차순: desc)

select 절

from 절

where 절

order by 절;


우리회사 사원들의 

사번, 성명, 부서코드, 급여 조회, 성명 순으로 조회

select employee_id, last_name||' '||first_name name, 

       department_id, salary

from employees

order by 2 desc;

--order by name desc;

--order by last_name||' '||first_name;


order by 절에 사용되는 기준: 컬럼표현, alias, select 목록에서 컬럼포지션

order by 절은 쿼리문의 가장 마지막에 위치한다.


우리회사 사원들의 

사번, 성, 부서코드, 급여 조회, 부서순으로, 급여 오름차순 조회

select employee_id, last_name, department_id dept, salary

from employees

order by 3 asc, salary ;

--order by dept;

--order by department_id;


'Database ORACLE' 카테고리의 다른 글

프로시저 procedure  (0) 2017.12.06
1130  (0) 2017.12.05
1127  (0) 2017.12.05
1122  (0) 2017.12.05
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함