Oracle : EquiJOIN

Yu Jung Eun·2023년 6월 9일
0

DBSQL

목록 보기
8/8

EQUIJOIN

두 개의 데이블간에 칼럼 값들이 정확하게 일치하는 경우 사용되며 대부분 PK, FK의 관계를 기반으로 한다.

EquiJOIN에서 WHERE을 조건을 제대로 쓰지 않으면 카티시안 곱과 같이 수많은 데이터를 가져오게 된다.

카티사인 곱은 다음과 같다.

카타시안 발생

SELECT employees.first_name, employees.department_id, departments.department_id, departments.department_name 
FROM employees, departments;

SELECT * FROM departments; -- 27개
SELECT * FROM employees; -- 111개의 데이터
--> 27 * 11 = 2997개의 결과
-- 카티시안 발생 이유 -> WHERE절 없음

다음과 같이 WHERE 조건문을 작성하지 않으면 무수히 많은 데이터가 출력되게 된다.

두 테이블 간 WHERE 조건문으로 연결하기

SELECT employees.first_name, employees.department_id, departments.department_id, departments.department_name 
FROM employees, departments
WHERE employees.department_id = departments.department_id; -- 두 테이블일 때엔 WHERE은 조건문은 하나.

다음과 같이 작성하면 두 테이블의 값을 칼럼 값이 일치하는 경우에 한해서 함께 출력할 수 있다.

여러 테이블을 연결하여 사용할 수도 있음

Seatttle에 근무하는 사원 중 급여 5000 이상인 사원**

테이블을 연결할 시에는 테이블 간 같은 칼럼을 갖고 있는 값끼리 WHERE 조건문을 사용하여 연결할 수 있다.

SELECT employees.first_name NAME, employees.salary SALARY, jobs.job_title JOB, employees.hire_date HIREDATE, employees.commission_pct BONUS,
departments.department_id DEPT, locations.city CITY
FROM employees, locations, departments, jobs
WHERE employees.salary >= 5000 
and locations.city = 'Seattle' 
and employees.department_id = departments.department_id 
and departments.location_id = locations.location_id
and jobs.job_id = employees.job_id
ORDER BY NAME;

업무명의 뒤에 Manager가 붙어있는 사람 조회

SELECT departments.department_name, employees.first_name, jobs.job_title
FROM employees, departments, jobs
WHERE employees.department_id = departments.department_id 
and jobs.job_id = employees.job_id
and job_title LIKE '%Manager';

만약 연결하고픈 테이블이 같은 컬럼을 가지고 있지 않다면?

Michael이라는 이름을 가진 사람의 정보

테이블 간 같은 컬럼을 가진 컬럼이 없다면 연결을 목적으로 하는 임의의 테이블을 하나 불러와 사용한다.

SELECT employees.first_name, locations.city, locations.state_province 
FROM employees, locations, departments
WHERE employees.department_id = departments.department_id 
and departments.location_id = locations.location_id
and first_name = 'Michael';
profile
미림마이스터고 유정은입니다 ◠‿◠

0개의 댓글

관련 채용 정보