<< Chapter < Page Chapter >> Page >
Project Operation Example - 2
  1. JOIN Operation

The JOIN operation is used to combine related tuples from two relations into a single tuple. The Theta-JOIN is a specialized product containing only pairs that match on a supplied condition called join-condition.

  • Notation: r join condition s size 12{r` ⊳ ⊲ rSub { size 8{ ital "join"` - ` ital "condition"} } s} {}
  • r C s = { ( t1 , t2 ) t1 r t2 s C ( t1 , t2 ) } size 12{r` ⊳ ⊲ rSub { size 8{C} } s`=` lbrace \( t1,t2 \) ` \lline t1 in r and t2 in s and C \( t1,t2 \) rbrace `} {} where r(R) , s(S)
  • Similar to PRODUCT, each tuple in the result of JOIN operation contains all attributes from two original relations. However, in this operation one tuple in R and one tuple in S can be combined together to form a tuple in the result if the combination satisfies the join condition.
  • Join condition is of the form:

<condition>AND<condition>AND …AND<condition>

where<condition>is a comparision between one attribute in R and one attribute in S, provided that these two attributes have the same domain.

  • Result size: r C s r s size 12{ \lline r` ⊳ ⊲ rSub { size 8{C} } `s \lline `<= ` \lline r \lline `*` \lline s \lline } {}
  • Result schema: If we have R(A1, A2, …, An) and S(B1, B2, …, Bm) then the list of attributes in Result is (A1, A2, …, An, B1, B2, …, Bm)
  • Producing the result of JOIN operation:
    • For each tuple in r, form new tuples by pair it with each tuple in s
    • If the new tuple satisfies the specified condition, then place it in the result set.
  • Example:
Join Operation Example - 1
Join Operation Example - 2
Join Operation Example - 3

Variations of JOIN

EQUI-JOIN: A JOIN where the only comparision operator used in the join condition is “=” is called EQUI-JOIN. The result of Equi Join always has one or more pairs of attributes that have identical values in every tuple.

Example:

Natural Join Operation Example - 1

NATURAL JOIN: The Natural Join operation is a specialised product where the result tuple contains only pairs of tuples that match on their common attributes with one of each pair of common attributes is eliminated. The standard definition of Natural Join requires that the two join attributes have the same name. Therefore, we can see that Natural Join is created to get rid of the duplicate columns in an Equi Join.

  • Notation: r * s
  • Natural Join can be defined using other operation r s = π R S ( σ condition ( r × s ) ) size 12{r*s=π rSub { size 8{r union s} } \( σ rSub { size 8{ ital "condition"} } \( r times s \) \) } {}

where r(R) and s(S) and condition is boolean expression (A1 = B1) AND (A2 = B2 ) AND … AND (Ak = Bk) with Ai is the attribute in r , Bi is the attribute in s and (Ai, Bi) is a pair of common attributes.

Producing the result of Natural Join

  • For each tuple in relation r, compare common attributes with those in each tuple of s
  • If two tuples match in their common attributes then combine tuples, remove duplicate attributes and add to the result.
  • Example: From the example of Equi Join, assume that the attribute list in s now is ( E, F,B) instead of (E, F, G) then we can have the expression r * s .
  • Retrieve the information of student who enrols in at least one course.
Natural Join Operation Example - 2
  1. DIVISION Operation

The Divition Operation is defined on two relation r(U1) and s(U2) where U2 is the subset of U1 and s is not an empty relation: r ÷ s = { t t r ( U 1 U 2 ) satisfy } size 12{r div s= lbrace t \lline t in r \( U rSub { size 8{1} } - U rSub { size 8{2} } \) and ital "satisfy" rbrace } {} where satisfy = t s s ( t r r ( t r [ U 2 ] = t s t r [ U 1 U 2 ] = t ) ) size 12{ ital "satisfy"= forall t rSub { size 8{s} } in s \( ` exists t rSub { size 8{r} } in r \( `t rSub { size 8{r} } \[ U rSub { size 8{2} } \] =t rSub { size 8{s} } ` and t rSub { size 8{r} } \[ U rSub { size 8{1} } - U rSub { size 8{2} } \]=t \) \) } {}

This means that for a tuple t to appear in the result of Division, the values in t must appear in r in combination with every tuple in s.

The Division is very useful for a special kind of query such as “ Retrieve the name of the student who enrolls in all course teach by Professor Ba”

  • Producing the result of the Division operation
    • Consider each subset of tuple in r that match on t[U1 – U2]
    • For this subset of tuples, take the values t[U2] from each. If this covers all tuples in s then add t[U1 – U2]in the result.
  • Example: Retrieve the name of subject that is taught in all courses
Division Operation Example

Data manipulation with relational algebra expression

Sample Database

In this session, we use the COMPANY database in the examples for illustrating the use of Relational Algebra for answering several queries.

The relational database schema for the COMPANY database is specified as below

EMPLOYEE (EID, Name, Bdate, Address, Salary, DeptId)

DEPARTMENT(DeptId, Dname, Office, Mng-EID)

PROJECT(Code, Name, Budget, DeptId)

JOIN(EID, PCode, StartDate)

EMP-DEPENDENT(EID, Dependent-Name, Bdate, Relationship)

Sample Queries

Query 1: Find all employees whose salary is greater than 30.000

σ salary > 30000 ( EMPLOYEE ) size 12{σ rSub { size 8{ ital "salary"`>`"30000"} } \( ital "EMPLOYEE" \) } {}

Query 2: Find the name, address of employees who works for department number 1

π Name , Address ( σ DeptId = 1 ( EMPLOYEE ) ) size 12{π rSub { size 8{ ital "Name", ital "Address"} } \( σ rSub { size 8{ ital "DeptId"`=`1} } \( ital "EMPLOYEE"` \) \) } {}

Query 3: Find the name of the department that employee John Smith works for.

π Dname ( σ Name = ' John Smith ' ( EMPLOYEE DEPARTMENT ) ) size 12{π rSub { size 8{ ital "Dname"} } \( σ rSub { size 8{ ital "Name"`=`' ital "John"` ital "Smith"'} } \( ital "EMPLOYEE"* ital "DEPARTMENT" \) \) } {}

Query 4: Find the EID, start date of the employees who works for project number P1 or project number P2

π EID , StartDate ( σ PCode = 1 PCode = 2 ( JOIN ) ) size 12{π rSub { size 8{ ital "EID", ital "StartDate"} } \( σ rSub { size 8{ ital "PCode"`=`1` or ` ital "PCode"`=`2} } \( ital "JOIN" \) \) } {}

Query 5: Find the name, relationship of all the dependents of employees who works for Department Human Resource

π Dependent Name ,Re lationship ( σ DName = ' Human Re source ' ( EMP DEPENDENT EMPLOYEE DEPARTMENT ) ) alignl { stack { size 12{π rSub { size 8{ ital "Dependent" - ital "Name"",Re" ital "lationship"} } \( σ rSub { size 8{ ital "DName"`=`' ital "Human"`"Re" ital "source"'} } } {} #\( ital "EMP" - ital "DEPENDENT"* ital "EMPLOYEE"* ital "DEPARTMENT" \) \) {} } } {}

Query 6: Finds the name of the employees who join in every project.

π Name ( EMPLOYEE ( π EID , PCode ( JOIN ) ÷ π Code ( PROJECT ) ) ) size 12{π rSub { size 8{ ital "Name"} } \( ital "EMPLOYEE"* \( π rSub { size 8{ ital "EID", ital "PCode"} } \( ital "JOIN" \) div π rSub { size 8{ ital "Code"} } \( ital "PROJECT" \) \) \) } {}

Query 7: Finds the names of the employees who have no dependent

π Name ( EMPLOYEE ( π EID ( EMPLOYEE ) EID ( EMP DEPENDENT ) ) ) size 12{π rSub { size 8{ ital "Name"} } \( ital "EMPLOYEE"* \( π rSub { size 8{ ital "EID"} } \( ital "EMPLOYEE" \) `\`π rSub { size 8{ ital "EID"} } \( ital "EMP" - ital "DEPENDENT" \) \) \) } {}

Query 8: Find the name of employees who works for both project number 1 and project number 2

π Name ( EMPLOYEE ( π EID ( σ PCode = 1 JOIN ) π EID ( σ PCode = 2 JOIN ) ) ) size 12{π rSub { size 8{ ital "Name"} } ` \( ital "EMPLOYEE"`* \( π rSub { size 8{ ital "EID"} } \( σ rSub { size 8{ ital "PCode"`=`1`} } ital "JOIN" \) ` intersection `π rSub { size 8{ ital "EID"} } \( σ rSub { size 8{ ital "PCode"`=`2`} } ital "JOIN" \) \) ` \) } {}

Query 9: Find the name of the manager who has at least one dependent

π Name ( EMPLOYEE ( π EID ( EMP DEPENDENT EID = Mng EID DEPARTMENT ) ) ) size 12{π rSub { size 8{ ital "Name"} } ` \( ital "EMPLOYEE"`* \( π rSub { size 8{ ital "EID"} } \( ` ital "EMP" - ital "DEPENDENT" ⊳ ⊲ rSub { size 8{ ital "EID"`= ital "Mng" - ital "EID"} } ` ital "DEPARTMENT" \) \) \) } {}

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Algebra. OpenStax CNX. Aug 17, 2012 Download for free at http://cnx.org/content/col11445/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Algebra' conversation and receive update notifications?

Ask