4일차 빅데이터

정준호·2022년 1월 14일
0

빅데이터

목록 보기
3/9
  1. 오답

  2. (DISTINCT 는 대문자로) 오답 유일한값=중복없음
    1 select DISTINCT job_id, department_id
    2 from employees
    3 where department_id in(30,90)
    4* order by job_id

  • 조인(join)기능
    여러개의 테이블에서 데이터를 가져오는방법
    조인이 발생하면 시스템내부적으로 조인대상 테이블의 행들을 하나의 행으로 조합 하게됨
    하나의 행으로 조합할때 반드시 무결성(정확한 행)이 보장이 되도록 행을 조합(did=did 같을때)=> 조인조건
    반드시 n(조인 테이블의 개수)-1개의 조인조건을 where절에 명시
    조인조건이 생략이 되거나 잘못 작성되었을때 카티션프로덕트 현상이 발생한다
    카티시안 프로덕트 - 가능한 모든 행의 조합을 시도

-select last_name, department_name
2from employees, departments
3where employees.department_id=departments.department_id

3번째 줄처럼명시해야 조인이 발생한다.

카티시안 프로덕트(Cartesian produc)

did가 같았을때 무결성이 보장이된다.

where절에 쓰인다
등가조인(equ) - 같은 데이터가 존재할때 (=) 조건이 맞지않으면 걸러진다.
비등가조인(non-equ) - 데이터가 전혀다를때
포괄조인(outer) - 어느한쪽 테이블의 데이터가 더많을때
자체조인(self) - 한개의 테이블을 대상으로 수행

테이블이름이 접두어로 붙어있다.
-틀린 명령어
1 select last_name, department_name, department_id
2 from employees, departments
3 where employees.department_id=departments.department_id
SQL> /
select last_name, department_name, department_id

ERROR at line 1:
ORA-00918: column ambiguously defined 틀린곳-department_id
-(두개의 테이블에 존재하는 컬럼이라 어디서 가져올지 몰라서 오류가 난다.)

  1. n-1 조인조건 -> where절에 명시 "from절에서 테이블 갯수-1개이다"
  2. d_id 똑같은 이름을 가진 컬럼을 명시할때 반드시 소유주 table_name을 접두어로 명시
  3. 최대 30자까지 쓸수있다.
    4. from절에도 알리아스를 쓸수있고 공백으로 사용한다.
    (테이블이름 나타내는법 1.풀네임 2. 알리아스)
  • n-1찾는법

1.등가조인

  • where절에 조인조건이 있을때 일반조건을 추가시키고싶으면 and를 써야한다.
  • 결과값이 같은곳에 쓰인다.
    select e.last_name, e.salary12 "AnnSal", d.department_name, d.location_id
    2 from employees e, departments d
    3 where e.department_id=d.department_id
    4
    and e.salary*12 >= 150000

2.아우터(outer)조인
더많은 데이터를 가지고있는 컬럼쪽에 붙여주면 반대쪽에 없는 데이터를 포함해서 들어간다.

  • select e.last_name, d.department_name, d.department_id
    from employees e, departments d
    where e.department_id=d.department_id(+) <--(+)아우터연산자

  • select e.last_name, d.department_name, d.department_id
    2 from employees e, departments d
    3* where e.department_id(+)=d.department_id
    두개의 결과값이 다르다.

예제)20. 커미션을 받는 사원들의 이름과 부서명, 지역명, 도시명을 알아내시오
select e.last_name, d.department_name, l.location_id, l.city
from employees e, departments d, locations l
where e.department_id=d.department_id
and d.location_id=l.location_id
and e.commission_pct is not null

profile
파이팅

0개의 댓글