[DB] SQL - 확장 문법

● 확장 문법

(1) 정렬 기능

기본 문법 order by 필드명[, 필드명,...] [asc|desc]

▶ 일반 정렬

 ex) 급여가 많은 순으로 사번, 이름, 급여를 조회

select empno, ename, sal from emp order by sal desc;


▶ 이차정렬

ex) 부서별로 급여가 적은 순으로 이름, 급여, 부서코드를 조회

select ename, sal, deptno from emp order by deptno asc, sal asc;




(2) 조건 기능(레코드 필터링)

기본 문법 where 조건문

조건문 : 필드명 연산자 값

연산자

기본 연산자 : >, <, >=, <=, =, (<>, !=, ^=)

논리 연산자 : and, or, not

SQL 연산자 : between and, in, is null, like


like의 wild card

% : 0개 이상의 문자를 대체

_  : 1개의 문자를 대체


ex) %beau% : beau, beauf, beauti, beauaaaaaa

beau_ : beau(x), beaua, beaub, beauc


ex) 급여가 3000$ 이상인 직원의 사번, 이름, 급여 조회

select empno, ename, sal from emp where sal >=3000;


ex2) 업무가 manager인 직원의 이름, 부서, 업무, 급여 조회

-> 데이터는 반드시 대소문자로 구분하며 호따옴표로 감싸줘야한다.

select ename, deptno, job, sal from emp where job = 'MANAGER';


ex3) 1982년 1월 1일 이후에 입사한 직원의 이름, 부서, 입사일자를 조회

select ename, deptno, hiredate from emp where hiredate >= '82/01/01';


ex4) 부서가 20이고 업무가 analyst인 직원의 이름, 부서, 급여, 입사일자 조회

select ename, deptno, sal, hiredate from emp where deptno=20 and job='ANALYST';


ex5) 급여가 1500이상 2500미만을 받는 직원의 이름, 부서, 업무, 급여를 조회

 단, 급여가 많은 순으로 조회

-> order by는 항상 끝에서 해줘야한다.(데이터가 확정된뒤 정렬해야하기 때문)

select ename, deptno, job, sal from emp where sal>=1500 and sal<2500 order by sal desc;


▶ 급여가 1500이상 2500이하일때.. (between ~and -> 이상/이하일때 사용가능)

select ename, deptno, job, sal from emp where sal between 1500 and 2500 order by sal desc;


ex6) 업무가 clerk, salesman, analyst인 직원의 이름, 부서, 업무, 급여를 조회

select ename, deptno, job, sal from emp where job = 'CLERK' or job = 'SALESMAN' or job = 'ANALYST';


▶ in연산자 사용 ( or, = 연산자를 사용할때만 in 연산자 사용 가능)

select ename, deptno, job, sal from emp 

where job in( 'CLERK', 'SALESMAN', 'ANALYST');


ex7) 보너스를 받지 못하는 직원의 이름, 부서, 업무, 급여, 보너스 조회

▶ is null 연산자 (null값이면 출력) 

select ename, deptno, job, sal, comm from emp where comm is null;


select ename, deptno, job, sal, comm from emp where job !='SALESMAN';

select ename, deptno, job, sal, comm from emp where not(job ='SALESMAN');


보너스를 받는 직원의 이름, 부서, 업무, 급여, 보너스 조회

select ename, deptno, job, sal, comm from emp where comm is not null;



ex8) 이름이 s로 시작하는 직원의 이름, 업무, 급여를 조회

▶ like 연산자(%사용)

select ename, job, sal from emp where ename like 'S%';




'DB' 카테고리의 다른 글

[DB] SQL 문제  (0) 2017.11.13
[DB] Function  (0) 2017.11.13
[DB] SQL - SELECT  (0) 2017.11.13
[DB] SQL  (0) 2017.11.10
[DB] DataBase 개념  (0) 2017.11.10

댓글

Designed by JB FACTORY