SQL: Fundamentals 1 : Chapter 2 - Restricting and Sorting Data
1장에서는 조건 없이 그냥 선택된 컬럼의 모든 것을 보여 주었다. 하지만 이
를 선택해서 보는 방법을 2장에서 배운다. 이미 학교에서 배운 것들이지만
워낙 재미 없었던 수업이라 기억이 안나는 것이 치명적 ㅡ,.ㅡ ;;;;
Limiting Rows Using a selection
select employee_id, last_name, job_id, department_id
from employees
where department_id = 90;
where절을 사용하여 department_id가 90인 사람만 뽑아 내는 SQL문장이다.
The default date dormat is DD-MON-RR(형식구별 필요)
Using the between condition
select last_name, salary
from employees
where salary between 2500 and 3500
2500과 3500사이의 값들이 검색이 된다. 주의 할것은 2500과 3500도 포함이 된다는것
또한 where last_name between 'king' and 'smith' 이것은 아스키 코드값으로 검색한다.
Using the IN condition
select employee_id, last_name, salary, manager_id
from employees
where manager_id in ( 100, 101, 201);
복수의 조건을 걸수 있는 in 조건이다. 100.101.201에 속하는 것들을 출력한다. 물론 문자도
같이 사용할수 있다.
Using the like condition
% denotes zero or many characters.
_ denotes noe character.
select first_name
from employees
where first_name like 'S%';
S로 시작하는 이름을 검색하겠다는 의미이다. 뒤의 문자는 어떤길이여도 상관없음
S_는 S로 시작하는 두글자로된 문자를 검색하는 의미가 된다.
Using the NULL conditions
select last_name, manager_id
from employees
where manager_id is null
이것은 null인 것을 찾으라는 의미가 된다. 이렇게 사용하는 이유는 앞에서 설명했듯이 manager_id = NULL 이라는것 자체가 넌센스 이기 때문이다. 그래서 오라클에서 제공하는
is null을 이용해서 검색해야 한다.
Logical Conditions
AND - returns true if both component conditions are true
OR - returns true if either component condition is true
NOT - Returns true if the following condition is false
select employee_id, last_name, job_id, salary
from employees
where salary >= 10000 and job_id like '%MAN%';
단순명료하게 두가지 조건에 모두 부합되는 사람을 찾으라는 문장이 된다.
salary는 10000이 넘고 job_id는 MAN이 들어가는 사람을 찾는 문장
모든 논리 조건이 조심해야 되는 것은 NULL값과의 조우시다.
false and NULL 이면 NULL이 아니라 false 가 된다.
이것은 and가 둘중 어느 하나라도 false면 다른 것이 무엇이든 간에 false가 되는 것을 떠올려 보면 어려운 이야기가 아니다 OR도 마찬가지로 생각해 주면된다.
Using NOT Operator
select last_name, job_id
from employees
where job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP')
NOT IN 에 들어가 있는 세가지 문자열을 빼고 검색하겠다는 조건이다 NOT은 반대라고 생각하면 되니 이것또한 별다르게 생각할것이 없다
Rules of Precedence
각각의 연산자에는 분명한 우선순위가 존재한다. 즉 여러 연산자를 함께 쓰게 되면 우선 연산 되어야 할 부분에는 괄호를 이용해 명시하는 방법을 사용하는 것이 현명하다.
Using the ORDER BY Clause
-ASC 오름 차순
-DESC 내림 차순
select last_name, job_id, department_id, hire_date
from employees
order by hire_date;
order by 절은 오름 차순과 내림차순으로 정렬해서 볼수 있게 하는 문장이다. 디폴트 값으로 오름 차순을 지원하고 있으며 내림차순으로 보고싶을 때에는 order by hire_date DESC
이렇게 표현하면 내림차순으로 출력해 준다. 이를 우리는 Sorting이라 한다.
또한 order by 절에는 select문에 포함되어 있는 않은 컬럼도 사용할수 있다.
Substitution Variables
치환변수는 where, order by, 컬럼 표현식, 테이블명, select 에 모두 사용할수 있다.
select employee_id, last_name, salary, department_id
from employees
where employee_id = &employee_num;
&를 사용하여 나타내 주게 되면 그 부분에 대한 값을 사용자가 직접 입력해주는 방식으로 사용할수가 있다. 즉 변수를 쿼리 문이 수행되는 도중에 입력해 줄수 있게 해주는 방식이다.
변수값이 문자이거나 날짜 일때에는 싱클 쿼테이션을 사용해 묶어 주어야 실행이 된다.
ex) where job_id = '&job_title'
&&를 사용하면 사용자에게 한번 물어보고 그 값을 저장해 두었다가 다음에 나왔을때 다시 물어보지 않고 전에 입력한 값을 넣어주는 방식이다.
select employee_id, last_name, job_id, &&column_name
from employees
order by &column_name;
이 SQL문장을 실행 시키면 처음에 컬럼의 이름을 물어보고 다음에 &column_name은 물어보지 않고 전에 저장했던 값을 대입해 결과값을 출력해 준다. 이렇게 저장된 값을 바꿔주어야 할때가 올수 있는데 그럴땐 UNDEFINE column_name 해주면 저장되어 있던 정보가 삭제되어 다음에는 다시 물어 보게 된다.
Using the iSQL *Plus DEFINE Command
변수값을 미리 저장해 놓고 나올때 마다 치환변수에 대입시켜주고 싶을때 사용한다.
define employee_num = 200
select employee_id, last_name, salary, department_id
from employees
where employee_id = &employee_num;
undefine employee_num
위의 define 사용예문에서 보면 알수 있듯이 미리 define 문으로 employee_num은 200이라 정의 하고 이를 환경변수에서 사용하는 모습을 볼수 있다. 마치 C언어의 #define 와 같은 기능이라 생각된다.
Using the VERIFY Command
SET VERIFY ON
select employee_id, last_name, salary, deprtment_id
from employees
where employee_id = &employee_num;
환경변수의 사용에 관하여 출력해 주는 명령어다 결과를 보면
old 3: where employee_id = &employee_num
new 3: where employee_id = 200
이렇게 나온다. 예전에는 old 였으며 바뀐뒤에는 new로 표현한다.
