select distinct * alais ( " " , , as )
from a, b
where
and
group by
정렬
select 문장 안에서 그룹화 현상이 일어나는 거
group by 는 그룹화 현상과 관련
join 조건이 들어가면 내부적으로 정렬작업 시작 > 여러개의 테이블을 하나의 테이블로 맥싱해줌
기능적 요구사항
결과
비기능적 요구사항
성능관련
과정
select 에는 내가 찾는 것만 적음
p265 하기위해
select salary
2 from employees
3 where last_name = 'Abel'
4 /
select last_name , salary
from employees
where salary >=11000
/
두개를 함 > 서브쿼리를 이용하여 한 번을 줄일 수 있다
select last_name , salary
from employees
where salary >=(
select salary
from employees
where last_name='Abel' )
/
서브쿼리
-조인을 해결한다
-모든 절에서 가능 (select , where , from 등등 / 가장많이 사용하는건 where인 셈)
-데이터 타입이 같아야한다 ( 문자면 문자 . 숫자면 숫자 )
-서브쿼리 안에 서브쿼리 사용가능 (개미지옥 .. ..)
-서브쿼리 내에서는 오드바이절에 사용 ㄴ
-Cuz 오드바이 절은 출력에 관한 거지만 서브쿼리는 정렬하고 끝나고 출력안해서
-탑앤분석 (최소값 몇개 구하기, 최대값 몇개 구하기) 에는 오드바이절 + 서브쿼리 같이 사용
-t1, t2 가 서
- 서브쿼리의 결과가 하나냐, 두개냐에 따라서 써야하는 연산자가 달라짐
결과 1 개 > 단일 연산자
결과 여러개 > in 사용 ( 하나 또는 여러개를 다 포함하기에 )
-메인에서 받아들이는 타입 =서브쿼리에서 넘겨주는 타입 ( 숫자인지 문자인지 같기 )
- 값이 없으면 서브 쿼리가 값을 반환하지 않음
단일 행 서브쿼리
다중 행 서브쿼리
멀티플 컬럼 서브쿼리
연산자 (4)
like
is null
between
in
t1 80번 부서의 평균 급여보다 더 많은 평균 급여를 받는 부서번호와 평균 급여를 출력하시오
select department_id, avg(salary)
from employees
group by department_id
having avg(salary) >
(
select avg(salary)
from employees
where department_id = 80
)
/
t2 부서별 평균급여보다 더 많은 급여를 받는 사원의 번호와 급여를 출력하시오
서브쿼리 실행 방벙
select
from
herer
select last_name, job_id, salary
from employees
where job_id = (
select job_id
from employees
where employee_id =141)
and salary > (
select salary
from employees
where employee_id = 143)
/
t 조인을 줄일 수 있고 간단 빨라서 현업에서 즐겨 찾음
select last_name, department_id
from employees
where department_id =
(
select depsrtet_id
from department
where department_name ='T'
)
/
t3 IT 이라는 부서에 근무하는 사원의 이름과 부서번호를 출력하시오
select last_name, department_id
from employees
where department_id =
(
select depsrtet_id
from department
where department_name ='T'
)
/
다중 행 서브 쿼리 p276
sal 10 20 30 40
in ( 30,40)
30 과 40
다 포함
any > ( 30,40)
30초과 > 40 50 60
최소값보다 큰값
any <( 30,40)
40 , 50, 60
최대보다 작은 값
all >( 30,40)
요소 전부보다 큰값
50, 60
all <( 30,40)
요소 전부보다 작은 값
10, 20
in
t 여러 행 서브 쿼리
select last_name , salary, department_id
from employees
where salary in (select min (salary)
from employees
group by department_id)
/
-
select employee_id, last_name, job_id, salary
from employees
where salary from employees
where job_id ='IT_PROG'
)
and job_id <> 'it_prog'
/
-
column last_name format a15
282p
연습문제
1
select last_name, hire_date
from employees
where department_id =(
select department_id
from employees
where last_name ='Zlotkey'
)
/
2
select employee_id, last_name, salary
from employees
where salary >
(
select avg(salary)
from employees
)
order by salary asc
/
3
select employee_id, last_name
from employees
where department_id in
(
select department_id
from employees
where last_name like '%u%'
)
4
select E.last_name ,E.department_id, E.job_id
from employees E
where E.department_id in
(
select D.department_id
from departments D
where D.location_id=1700
)
/
5
sol1
select last_name, salary
from employees
where manager_id in
(
select manager_id
from employees
where last_name= 'King'
)
/
> 경우 king 이름을 가진 다른 사원에대한 결과값도 출력됨
select last_name, salary
from employees
where manager_id =
(
select employee_id
from employees
where manager_id is null
)
/
> 보스만의 결과값만 출력되지만, 보스의 manager_id 가 null 인것을 사전에 알고 있어야 가능하다
6
select department_id, last_name, job_id
from employees
where department_id in
(
select department_id
from departments
where department_name like 'Executive'
)
/
7
>> 수정하기
select employee_id, last_name, salary
from employees
where department_id in
(
select department_id
from employees
where last_name='%u%'
and salary > avg(salary)
)
_______________ 8장 317p
insert into table
-한번에 하나의 행만 작성 가능
-테이블의 마지마겡 추가됨
-주의사항 : 에러가 되게 많이 난다
-괄호로 순서 입력 순서 가능
-실제 데이터 값을 입력할때 주의사항
-PK, FK 의 제약조건에 맞는 것 작성
-타입맞춰 작성
-null 값 작성하는 방법
-insert into dept( a, b, c) 라면 4자리 부터 null값입려됨
즉 공백시 null값됨
-insert into dept( a, b, c, null) 직접입력함
단 'null' 시 문자가 됨
values
입력값
insert into 의 컬럼의 수와 타입만 맞춰주면 된다
insert into에 괄호로 순서 바꾼다면 그대로 작성하면 된다
insert into departments
values(280, 'test', 100, 1700)
/
> 전체 추가
insert into departments(department_id, department_name )
values (290, 'test1' )
/
> 일부& 널 값 추가
select sysdate from dual
/
> 현재 날의 날을 반환해주는 함수
sysdate
> 그때마다 값을 계산해서 돌려주는 함수
> 디폴트값이라고 치환한다
날짜값
p327
생성 1
QL> create table test3
2 as select *from departments;
Table created.
SQL> truncate table test3;
Table truncated
SQL> select *from test3;
no rows selected
SQL> in sert into test3
SQL> select *from departments
2 /
생성 2
create table annsal
(deptid number(4),
annsal number(8,2));
생성3
create table empannsal
as select employee_id , salary from employees;
생성 4
truncate table empannsal;
-
desc annsal
-
insert into annsal
select department_id, sum(salary)
from employees
group by department_id
/
12 rows created.
SQL> select *from annsal;
DEPTID ANNSAL
---------- ----------
100 51608
30 24900
7000
90 58000
20 19000
70 10000
110 20308
50 156400
80 304500
40 6500
60 28800
DEPTID ANNSAL
---------- ----------
10 4400
12 rows selected.
Wrote file afiedt.buf
1 insert into empannsal
2 select employee_id, 12*salary
3* from employees
4 /
107 rows created.
SQL> select *from empannsal;
>확인가능
update 문 p329
-업데이트는 컬럼단위로 이뤄진다
-where 사용시 조건에 맞는 것만 변경되지만
없이 사용시 전부가 변경된다
update employees
set salary =200000
> select salary *from employees
테이블 행 삭제
-delete from ~
-where 사용시 조건에 맞는 것만 변경되지만
없이 사용시 전부가 변경된다
작업취소
rollback;
insert
[Data Base] 2019.06.24. 기록 (0) | 2019.08.24 |
---|---|
[Data Base] 2019.06.22. 기록. 실습내용 (0) | 2019.08.23 |
[Data Base] 2019.06.20. 기록 (0) | 2019.08.22 |
[Data Base] 2019.06.19. 기록 (0) | 2019.08.21 |
[Data Base] 2019.06.18. 기록 (0) | 2019.08.20 |
댓글 영역