(오라클) View(뷰) + where/order by/like/is/() / in 사용방법 예제

먼저  hr 계정에 접속하자,
hr 계정에 접속한뒤, hr에 이미 들어있던 테이블을 볼려면,

select * from tab;




이제  VIEW 라는 걸 볼것이다.
그럼 VIEW 라는게 무엇일까?




select ~ from @

@ 이 안에는 뭐가 들어갈수 있을까?
1. table view
2. virtual table
들만 들어갈수 있다.

~안에는 @이라는 테이블 컬럼 리스트를 추가적인 컬럼을 생성할수 있다.
그러니까 새로운 컬럼을 생성할수 있다는 이야기다.




select * from employees;




이렇게 뜬다

*가 모든 컬럼 명 이란 뜻이다.
만약 성명과 이메일만 보고싶다면

select first_name, last_name, email from employees;

라고 하면 된다.


select '회사명 : (주)상점', emp.*from employees emp;

을 적으면 employee_id 앞에, '회사명: (주)상점' 이라는 컬럼들이 생성된다.



select employee_id, first_name || ' ' || last_name 성명, email as 이메일, phone_number, salary, department_id from employees;


|| ' ' ||은 first_name 과 last_name을 이어주는것이다.

blank (공백)도는  as~ 은 닉네임을 주는것이다.
그럼 email>>이메일로 바뀌고,
first_name || ' ' || last_name >> 성명 이라고 바뀐다.




salary란 뜻은 1달치 급여다
wage 란 뜻은 1년치 급여다.

salary*12 -> wage 라는 뜻이다

select employee_id, first_name, salary*12 wage from employees;



salary >> wage로 되었다.


이제 wage에 조건문을 붙혀보자

select employee_id, first_name, salary*12 wage from employees where salary*12 >= 10000;

where~ 은 조건문이다

salary*12>= 10000;

10000달러보다 높은 급여들을 필터해서 볼수있게 해주는 것 이다.


이렇게 10000이상 급여를 받는 사람만 볼수있다.


만약 조건문을 두개 붙히고 싶으면 and 를 붙히면 된다

select employee_id, first_name, salary*12 wage from employees where (salary*12 <=99999 and salary*12 > 90000) ;




select employee_id, first_name, salary*12 wage from employees where salary*12 >= 10000 order by first_name;

o

우린 또 조건물을 붙혔다.

order by first_name;  -> order by는 근데 과부하 가 잘 걸린다....ㅠㅠ

first_name의 오른차순 정렬 을 하는것이다.

내린차순 정렬은

select employee_id, first_name, salary*12 wage from employees where salary*12 >= 10000 order by first_name desc;

w->a 로 순서대로 정렬된다.


근데, 만약에 first_name이 똑같이 중복되면 좀 데이타 관리가 힘드니까,
우린 이름으로 필터링 안하고 , 사원 번호로 필터링을 많이한다

first_name >> employee_id

로 사용 해주면 좋겠다.


select * from employees where first_name != 'Steven' ;

스티븐만 빼주고, 모든 사원을 볼수있게 해주는 것,




Steven 과 steven 은 다르다
왜냐면, 데이터에서는 대소문자를 구별해주기 때문이다.




steven 이라 하면 사라지지 않는다...... 흑흑흑.



또한 어떤 특정한 칼럼 값 이 null인 값을 찾을려고 하면?

select *from employees where commission_pct is null;




성이 A부터 시작하는 단어를 찾을때,
select * from employees where first_name like 'A%'

성이 N올긑나는 단어를 찾을때

select * from employees where first_name like '%n'


성이 n이 들어가는 이름을 찾을때
select * from employees where first_name like '%n%'





고오급 기술


hr에 departments 라는 테이블이 또 있다.


이제 문제는

department_id을 모르고, department-name 만 알때, marketing 부서에 있는 인원들의 자료를 뽑아오는 것을 할것이다.


답은,

select * from employees
    where DEPARTMENT_ID = (select DEPARTMENT_ID from DEPARTMENTS where DEPARTMENT_NAME = 'Marketing');


자 이렇게 2명이 뽑혔다.


select* from employees -> employees 테이블 안에서 모든 정보를 뽑아온다
where department_id =  -> department_id 는
(select department_id from departments where department_name = 'marketing');
-> department 테이블에서 department_id를 뽑아오는데 department name이 마케팅인것을 뽑아와라 하는것이다.


substitution method이라고 생각하면 된다
대입 방법!


만약에 부서이름이 marketing만 아니고 sales도 있다 생각하면,
우리는 이제 = 을 못쓴다 왜냐하면 이건 일대일 방식이기 때문이다.


그래서 우리는 in을 쓴다

select * from employees 
where department_id in(select department_id from departments 
where department_name in ('Marketing', 'Sales')) ; 




댓글

이 블로그의 인기 게시물

c++ 랜덤 숫자 생성하기 / 컴퓨터 난수 시드 설정

(오라클) Sequence/시퀸스의 활용과 개념