2020. 5. 1.

[Oracle] 8. 조인 : 등가 조인(equi joun)과 비 등가 조인(Non equi-join) 정리


등가 조인과 등가 조인(euqi join, nequi join : inner join)

SQL> select 테이블1.컬럼, ….테이블2.컬럼… from 테이블1, 테이블2, where 조인_조건 and 일반_조건

  • 조인 조건은 테이블간의 관계를 수식으로 표현한 것이다.
  • 조인 조건은 select 문에 ㅏ용된 테이블의 개수에 따라 달라지지만 테이블 n 라면 조인 조건의 개수는 항상 (n-1) 이상이다.
  • 조인 조건에 '=' 이용하는 조인을 등가 조인(equi-join)이라고 하고 부등호가 포함된 조인 조건을 이용하는 경우 등가 조인(non equi-join)이라고 한다.

예제 1. 사원의 근무 부서를 검색한다.

SQL> select eno 사번, ename 이름, emp.dno 부서번호, dept.dno 부서번호, dname 부서명 from dept,emp where dept.dno = emp.dno;


예제 2. 광주에서 근무하는 직원의 명단을 검색한다.(부서번호와 부서명도 검색한다.)

SQL> select loc 근무처, d.dno 부서번호, dname 부서명, eno 사번, ename 이름 from dept d, emp e where d.dno=e.dno and loc='광주';


예제 3. 직원의 급여를 10% 인상한 경우 급여 등급을 검색한다.

SQL> select eno 사번, ename 이름, sal*1.1 인상된급여, grade 등급 from emp,salgrade where sal * 1.1 between losal and hisal;


예제 4. 조인 조건이 없는 잘못된 조인문장의 결과를 확인해 보자.

SQL> select d.dno, dname, e.dno, ename from dept d, emp e;


예제 4) 같은 조인을 cross join(교차 조인)이라고 하는데 이런 조인은 특별한 경우 사용한다. cross join 잘못된 결과값(cartesian product) 생성한다.

테이블과 관계

  • 부모 테이블과 자식 테이블


실습

 1. 송강 교수가 강의하는 과목을 검색한다.

select p.pname, c.cname from professor p, course c where p.pno=c.pno and p.pname='송강'

#자료를 동기화 시키기 위해서는 pno으로 연결해줘야한다.


 2. 과목명에 화학이 포함된 과목을 강의하는 교수의 명단을 검색한다.

select p.pname, c.cname from professor p, course c where p.pno = c.pno and c.cname like '%화학%';


 3. 학점이 2학점인 과목과 이를 강의하는 교수를 검색한다.

select p.pname, c.cname, c.st_num from professor p, course c where p.pno=c.pno and c.st_num =2;


 4. 화학과 교수가 강의하는 과목을 검색한다.

select p.pname, c.cname, p.section from professor p, course c where p.pno=c.pno and p.section ='화학';


 5. 화학과 1학년 학생의 기말고사 성적을 검색한다.

select t.sname, t.syear, s.result from student t, score s where t.sno = s.sno and t.major ='화학' and t.syear=1;


 6. 일반화학 과목의 기말고사 점수를 검색한다.

select c.cname, s.result from score s, course c where s.cno=c.cno and c.cname='일반화학';


 7. 화학과 1학년 학생의 일반화학 기말 고사 점수를 검색한다.

select t.sname, t.major, t.syear, s.result from course c, score s, student t where s.sno=t.sno and c.cno=s.cno and c.cname='일반화학' and t.syear=1 and t.major='화학';

# course student 까지 연동하려면 가운데 score 테이블이 있어야 한다.
# 그래야 일반화학인 들어간 cname 컬럼을 가져올수 있다.


키는 score 연결고리가 있다.

 8. 화학과 1학년 학생이 수강하는 과목을 검색한다.

select distinct c.cname, t.major, t.syear from course c, student t, score s where s.sno=t.sno and c.cno=s.cno and t.major='화학' and t.syear=1;


 9. 일반화학 과목에서 평가 점수가 A 학생의 명단을 검색한다.

select t.sname, c.cname, s.result from course c, student t, score s where s.sno=t.sno and c.cno=s.cno and s.result between 90 and 94 and c.cname = '일반화학';


 10. 송강 교수의 과목을 수강하는 학생의 기말고사 점수를 성적 순서로 검색한다.

select t.sname, s.result from course c, student t, score s, professor p where s.sno=t.sno and c.cno=s.cno and p.pno=c.pno and p.pname='송강' order by s.result desc;



 11. 화학과 1학년 학생의 기말고사 성적을 학점(A, B, C, D, F)으로 검색한다.

select t.sname, t.syear, c.cname ,
decode (trunc(s.result,-1) , 100, 'A'
                        ,  85, 'A'
                        ,  70, 'B'
                        ,  55, 'C'
                        ,  40, 'D'
                            ,  'F' )
from course c, student t, score s, professor p
where s.sno=t.sno and c.cno=s.cno and p.pno=c.pno and t.major = '화학' and t.syear=1 order by t.sname;


# decode 치환함수로 100부터 90까지는 A 90부터 80까지 B 들어가고
# trunc 소수점을 떼버리기 위해서 함수이다.
# 다하고 나중에 알았다. scgrade 테이블의 정보를 쓰라는거였는데 귀찮아서 스킵


 12. 송강 교수가 강의하는 과목에서 평가 점수가 A 학생의 명단을 과목명과 함께 검색한다.

select t.sname, c.cname
from course c, student t, professor p, score s
where s.sno=t.sno and c.cno=s.cno and p.pno=c.pno
and p.pname='송강' and s.result >= 90 ;


 13. 화학과 1학년 학생에게 강의하는 교수의 명단을 검색한다.

select distinct p.pname
from course c, student t, professor p, score s
where s.sno=t.sno and c.cno=s.cno and p.pno=c.pno
and t.major='화학' and t.syear=1;


 14. 양선호의 입사일보다 빨리 부임한 교수의 명단을 검색한다.

select p.pname, p.hiredate from professor p, emp e where ename in('양선호') and e.hdate > p.hiredate;



직원 테이블에서 가져와야함

댓글 없음:

댓글 쓰기