ORACLE


5) where 절 : 검색에 조건을 부여함

(문제) 급열가 1000보다 많고 4000보다 작은 직원 검색하기 (급여 내림차순)

select * from emp where sal > 1000 and sal < 4000 order by sal desc;

 

6) 연산자의 종료

가. 산술션산자 : +, -, *, /

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

다. 논리연산자 : and, or, not


(문제) 급여가 1000 이하 또는 4000 이상의 직원 검색하기(급여 내림차순)

 

select * from emp where not(sal > 1000 and sal < 4000 ) order by sal desc;

-- not 
-- 100 이하, 400 이상
-- from(테이블) => select(필드)  => where(행, 레코드) => order by(sort, 정령)

select * from emp where not( sal > 100 and sal < 400) order by sal desc;

 


라. SQL 연산자 : in, any, all, between, like, is null, is not null


-- in 연산자

select deptno, sal, ename from emp where deptno in(10, 20, 100 ); // or

 


--  or 로 풀어서

 

slect * from emp where 

deptno =10 or deptno = 20 or deptno= 100

order by deptno , ename;

 


-- any 연산자 = 이 들어간다. any(집합) 집합 중 하나 이상 만족하는 것

 

select deptno, sal, ename from emp where dptno =any(10, 20, 100) ; // in과 같으나 연사자를 사용함

 

-- all 연산자 : all (집합) 집합의 모든 요소를 만족하는 것

select deptno, sal, ename from emp where dptno =all(10, 20, 100) ;


select sal, ename from emp where sal between 3000 and 5000;

 

 


-- like '키워드%' 키워드로 시작하는 필드

select ename from emp where ename like 'A%'; 


-- like '%키워드%' 키워드가 포함된(위치에 관계x) 필드

select ename from emp where ename like '%T%';


-- _1글자

select ename from emp where ename like '_L%'; (_: 1개의 문자열)

 


-- null 과의 연산결과는  =>null


select ename from emp where comm is null;

 

-- nvl(A,B) A가 null 이면 B, A 가 null 이 아니면 A

 

select ename, sal, comm, nvl(sal*12+comm, sal*12) 연봉 from emp;

 

-- in 연산자

slect * from emp where deptno in (10, 20, 100) order by deptno , ename;

==> 

slect * from emp where 

deptno =10 or deptno = 20 or deptno= 100

order by deptno , ename;


-- 잘못된 표현 (null과의 연산자 사용 => null)

select * from emp where comm = null;


-- 0과 null 은 다름
select * from emp where comm = 0;


-- 컬럼 is null, 컬럼 is not null

select * from emp where comm is null; -- null 인 직원

select * from emp where comm is not null;  -- null이 아닌 직원

 

 

 

마. 결합연산자 : ||

** 결합할 내용이 날짜나 문자인 경우에는 단일따옴표('')를 붙임


(문제)각 사람의 급여를 검색해서 '누구누구의 급여는 얼마입니다' 로

컬럼명을 만들어서 출력하기

select ename || '의 급여는  ' || sal || '입니다' from emp;


-- A || B 결합연산자 : A와 B가 문자열로 연결됨

select ename || ' ' || job from emp;

select FIRST_NAME || '의 급여는 ' || SALARY || '입니다' from emp;


7) 연산자 우선순위 **괄혼() : 연산자 우선순위보다 우선함

1순위 : 비교연산자, SQL연산자, 산술연산자

2순위 : not

3순위 : and

4순위 : or

5순위 : 결합연산자

(비교예제)


-- 연산자의 우선순위 () 가 우선적으로 처리됨

select JOB_ID, salary from emp

    where not(salary   > 1000 and salary <3000 ) order by salary;

 select JOB_ID, salary from emp where not salary >1000 and salary <3000 order by salary;


<실습문제>

1. Emp 테이블에서 입사일(hiredate) 이 2005년 1월 1일 이전인 사원에 대해 사원의 이름(ename)

, 입사일, 부서번호(deptno)를 나타내시오


select  FIRST_NAME, HIRE_DATE, DEPARTMENT_ID from emp where HIRE_DATE < '2005-01-01' ;


2.Emp 테이블에서 부서번호가 20번이나 30번인 부서에 속한 사원들에 대하여 이름, 직업코드(job_id)
, 부서번호를 나타내시오.
  
select ename, job, deptno from emp where deptno=20 or deptno =30;


-- JOIN

-- oracle 방식

select ename, job, dname from emp e, dept d 

    where e.deptno=d.deptno;

-- ANSI sql 방식 모든 sql 의 표준방식

select ename, job, dname from emp e join dept d
 
 on e.deptno =d.deptno;


-- view 생성(가상테이블)

-- create or replace view 뷰이름 as ( select 쿼리문 )

create or replace view emp_dept_v as

select e.ename, e.job, d.deptno, d.dname from emp e, 

dept d where e.deptno=d.deptno;

-- 데이터 사전( data dictionary ) 확인

select * from tab;

-- 뷰를 테이블처럼 사용

select * from emp_dept_v;

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

젖 떨어진 강아지 같다 , 몹시 보챈다는 말.

댓글 ( 4)

댓글 남기기

작성