Search

JOIN, 서브쿼리 함수

NATURAL JOIN

select employee_id, first_name, job_id, job_title from employees NATURAL JOIN jobs; # NATURAL JOIN을 사용하면 다른 테이블을 불러올 수 있는데, 컬럼의 이름이 같지 않을 때만 가능 # NATURAL JOIN은 중복하는 컬럼이 하나일 때만 제대로 사용할 수 있다.
SQL
복사
select employee_id, first_name, job_id, job_title from employees NATURAL JOIN jobs where department_id IN (20,50); # department_id가 20이거나 50인 사원만 출력
SQL
복사

JOIN USING

select * from employees JOIN departments USING (department_id); # 그냥 JOIN을 쓸 때는 어떤 칼럼으로 기준을 잡을지 정해줘야 한다. # NULL 값은 제외하고, 출력
SQL
복사
select ename, loc from emp JOIN dept USING (deptno); where loc = 'DALLAS'; # 위치가 DALLAS인 사원 이름 출력
SQL
복사
select ename, sal, job, loc from emp JOIN dept USING(deptno) where job = 'SALESMAN' # 직업이 SALEMAN인 사원의 이름, 급여, 직업, 위치 출력
SQL
복사
select ename, sal, job, loc from emp JOIN dept USING(deptno) where sal >= 3000 # 급여가 3000 이상인 사원의 이름, 급여, 직업, 위치 출력
SQL
복사
select d.loc, SUM(e.sal) from emp e JOIN dept d USING(deptno) GROUP BY d.loc; # 위치별 토탈급여 출력
SQL
복사
select d.loc, SUM(e.sal) from emp e JOIN dept d USING(deptno) where loc != 'DALLAS' GROUP BY d.loc; # DALLS를 제외한 위치별 토탈급여 출력
SQL
복사
select SUM(CASE loc WHEN 'NEW YORK' THEN sal END) as "NEW YORK", SUM(CASE loc WHEN 'DALLAS' THEN sal END) as "DALLAS", SUM(CASE loc WHEN 'CHICAGO' THEN sal END) as "CHICAGO" from emp JOIN dept USING(deptno); # 위치별 토탈 급여 가로로 출력
SQL
복사
select last_name from employees JOIN departments USING (department_id) where location_id = (select location_id from locations where state_province = 'California'); # davies와 같은 주에 있는 사원 구하기
SQL
복사
select e.last_name from employees e JOIN departments d USING (department_id) JOIN locations l USING (location_id) where l.state_province = (select state_province from locations where location_id = (select location_id from departments where department_id = (select department_id from employees where last_name = 'Davies'))); # Davies와 같은 주에서 근무 중에 사원의 last_name 출력
SQL
복사

JOIN ON

select employee_id, department_name from employees emp JOIN departments dept ON (emp.department_id = dept.department_id); # ON은 이렇게 alias를 지정하여, 어느 테이블을 기준으로 잡을지 정한다. # ON은 칼럼 이름이 서로 다른데, 그 칼럼으로 기준을 잡고 싶을 때 사용한다.
SQL
복사
select ename, loc from emp e JOIN dept d on e.deptno = d.deptno; where loc = 'DALLAS'; # 위치가 DALLAS인 사원의 이름 출력
SQL
복사
select e.ename, e.sal, s.grade from emp e JOIN salgrade s ON (s.losal <= e.sal and s.hisal > e.sal); # 급여로 사원들의 등급 출력
SQL
복사
select e.last_name from employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id where state_province = 'California' # # California 주에서 근무하는 사원의 last_name 출력
SQL
복사

three away JOIN USING

select employee_id, department_name, city from employees JOIN departments USING (department_id) JOIN locations USING (location_Id); # 사원들의 employee_id, department_name, city 출력
SQL
복사
select e.empno, d.department_name, l.city from emp e JOIN departments d ON e.deptno = d.department_id JOIN locations l USING(location_id); #
SQL
복사

three away JOIN ON

select employee_id, department_name, city from employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id; # 사원들의 employee_id, department_name, city 출력
SQL
복사

SLEF JOIN

select e1.first_name, e2.first_name as MANAGER_NAME from employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id; # 사원들의 이름과 그 사원들의 MANAGER 이름 출력
SQL
복사
select e1.ename, e2.ename as MGR from emp e1 JOIN emp e2 ON e1.mgr = e2.empno; # 사원들의 이름과 그 사원들의 MANAGER 이름 출력
SQL
복사
select e1.first_name, e2.first_name as MGR, e3.first_name as Admin from employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id JOIN employees e3 ON e2.manager_id = e3.employee_id; # 사원들의 이름과 그 사원들의 MANAGER의 이름과, 그 MANAGER에 Admin 이름 출력
SQL
복사

서브 쿼리

select * from employees; select last_name, hire_date from employees where hire_date > (select hire_date from employees where last_name = 'Davies'); # Davies보다 늦게 입사한 사람의 성과 고용일 출력
SQL
복사
select last_name, department_id from employees where department_id = (select department_id from employees where last_name = 'Davies'); # Davies와 같은 부서인 사람들의 last_name과 department_id 출력
SQL
복사
select last_name from employees where department_id = (select department_id from departments where location_id = (select location_id from locations where state_province = 'California')); # California 주에서 근무하는 사원의 last_name 출력
SQL
복사