본문 바로가기

데이터베이스

DBMS - (15) join operator

DBMS - (15) join operator

Cross Product == X
Inner Join == θ
Natural Join
Inner Join using(attr) == θ
Left | Right | Full outer join

<그림 1: relation example>

Inner Join
Q1)
select distinct sName, major
from Student, Apply
where Student.sID = Apply.sID;
 
Q2)
select distinct sName, major
from Student [inner] join Apply
on Student.sID = Apply.sID;

Q1과 Q2의 쿼리 결과는 동일하다

Q3)
select sName, GPA
from Student, Apply
where Student.sID = Apply.sID
and sizeHS < 1000 and major = 'cs' and cName='stanford';

Q4)
select sName, GPA
from Student join Apply
on Student.sID = Apply.sID
where sizeHS < 1000 and major = 'cs' and cName='stanford';

Q3과 Q4의 쿼리 결과는 동일하다

3개 이상의 릴레이션 간 Join
Q5)
select Apply.sID, sName, GPA, Apply.cName, enrollment
from Apply, Student, College
where Apply.sID = Student.sID and Apply.cName = College.cName;

Q6)
select Apply.sID, sName, GPA, Apply.cName, enrollment
from (Apply join Student on Apply.sID = Student.sID) join College on Apply.cName = College.cName;

Q5과 Q6의 쿼리 결과는 동일하다

Natural Join
Q7)
select distinct sName, major
from Student inner join Apply
on Student.sID = Apply.sID;

Q8)
select distinct sName, major
from Studnet natural join Apply;

Q7과 Q8의 쿼리 결과는 동일하다

Inner join using(attr)
Q9)
select distinct sName, major
from Student join Apply using(sID);

Q8과 Q9의 쿼리 결과는 동일하다. 그러나, natural join은 데이터베이스 연산시 동일한 attr 이름을 찾기 위해 연산 비용이 추가로 요구되나 using(sID)로 명시적으로 표현한 경우에는 이러한 비용이 소모되지 않으므로 성능상 유리하다.

Q10)
select S1.sID, S1.sName, S1.GPA, S2.sID, S2.sName, S2.GPA
from Student S1, Student S2
where S1.GPA = S2.GPA and S1.sID <> S2.sID;

Q11)
select S1.sID, S1.sName, S1.GPA, S2.sID, S2.sName, S2.GPA
from Student S1 join Student S2 using(GPA)
on(=where) S1.sID <> S2.sID;

Q10과 Q11의 쿼리 결과는 동일하다.

Outer Join
1. Left outer join
Q12)
select sName, sID, cName, major
from Student left outer join Apply using(sID);

<그림 2: Left outer join>

Q13)
select sName, sID, cName, major
from Student, Apply
where Student.sID = Apply.sID
union
select sName, sID, null, null
from Student
where sID not in (select sID from Apply);

Q12와 Q13의 쿼리 결과는 동일하다. outer join은 두 피연산자 릴레이션간 Join시 매칭되는 attr이 없더라도 null value를사용하여 연산 결과를 반환하는 특징을 지닌다.

2. Right outer join
Q14)
select sName, sID, cName, major
from Student right outer join Apply using(sID);

<그림 3: Right outer join>


Q15)
select sName, sID, cName, major
from Student, Apply
where Student.sID = Apply.sID
union
select null, sID, cName, major
from Apply
where sID not in (select sID from Student);

Q14와 Q15의 쿼리 결과는 동일하다.

3. Full Outer join
Q16)
select sName, sID, cName, major

from Student full outer join Apply using(sID);

Q17)
select sName, sID, cName, major
from Student, Apply
where Student.sID = Apply.sID
union
select null, sID, cName, major
from Apply
where sID not in (select sID from Student)
union
select sName, sID, null, null
from Student
where sID not in (select sID from Apply);

Q16과 Q17의 쿼리 결과는 동일하다.

교환법칙 : A op B = B op A
- left, right outer join을 제와한 모든 연산이 교환법칙을 만족한다.

결합법칙 : (A op B) op C = A op (B op C)
- outer join을 제외한 모든 연산이 결합법칙을 만족한다.

Reference
https://www.youtube.com/watch?v=oXd4mTA86MI&list=PL6hGtHedy2Z4EkgY76QOcueU8lAC4o6c3&index=18

'데이터베이스' 카테고리의 다른 글

DBMS - (17) functional dependency  (0) 2019.08.15
DBMS - (16) relational design overview  (0) 2019.08.15
DBMS - (14) data modification statement  (0) 2019.08.04
DBMS - (13) null value  (0) 2019.08.04
DBMS - (12) aggregation function  (0) 2019.08.04