You are on page 1of 7

Q3. Describe Projection operation, Set theoretic operation & join operation?

Project operation: Projection operation is used to select only few columns from a table. the mathematical symbol p<ATTRIBUTE LIST>(<relation>) Here, <attribute list> is a list of attributes from the relation r hence the degree (number of columns) of the result is equal to the number of attributes specified in the attribute list. Eg 1. Select the name and salary of all the employees. pNAME. SALARY (EMPLOYEE). This query selected only name and salary attributes from relation EMPLOYEE Eg. 2. Select names and addresses of all employees working for department 10. pNAME, ADDRESS (DNO=10(EMPLOYEE) Set theoretic operations: These are used to merge the elements of two sets in various ways, including union, intersection and difference. Three of these operations require the table to be union compatible. The two relations are said to require the table to be union compatible. The two relations are said to be union compatible if the following conditions are satisfied. 1. The two relation/tables (say R & S) that have the same number of columns (have the same degree) 2. Each column of the first relation/table must be either the same data type as the corresponding column of the second relation/table(s). Relations R & S

Intersection (?): The intersection operation selects the common tuples from the two relations. The result of the operation R?S is

Union (

):

The result of this operation denoted by RS, is a relation that includes all tuples that are either in R or in S or in both. Duplicate tuples will not appear in the output.

Difference ( ): The result of the difference consists of all tuples in R but not in S

Cartesian products (X): The Cartesian product or cross-product is a binary operation that is used to combine two relations. Assuming R & S as relations with n and m attributes respectively, the Cartesian products R x S can be written as, R (A1, A2..An) x S (B1, B2.Bn) The result of the above set operation is Q (A1, A2..An, B1, B2.Bn) Total number of columns in Q: degree (Q) = n + m Total number of tuples in Q: count (Q) = Number of tuples in R* Number of tuples in S

Cartesian product of R and S can be written as,

The relation R has 2 columns and 3 tuples. The relation S has 2 columns and 3 tuples. So the Cartesian product has 4 columns (2+2) and 6 tuples (3 x 2). The Cartesian product operation applied by itself is generally meaningless. It is useful only when followed by selection and projection operations.

Renaming r (rho): This operation is used to rename the relations or attributes. The symbol r(rho) is used to denote the rename operator. In some situations, it is better to break down a complex query into two or more simple querys. We must rename the relations that hold the intermediate result relations. It improves the readability and facilitates better understanding. The syntax is as follows: Rename <OLD TABLE> to <NEW TABLE>

Here S is new new relation and R is original relation.

6.4.4 The Join Operation Join ( ): The capability of retrieving data from multiple tables using a single SQL statement is one of the most powerful and useful features of RDBMS. It is the availability of join operation. We know that one table may not give all the information about a particular entity. The join operation, denoted by is used to combine two relations to retrieve useful information. A join operation matches data from two or more tables; based on the values of one or more columns in each table, it allows us to process more than one table at a time. For e.g.: The employee table gives only the department id's, if we want to know the department name, then we have to get the information by joining employee table and dept. table. In join, only combinations of tuples satisfying the join condition appear in the result. The general form of a Join operation is R<join condition>S For example by joining employee and department relations, we can get the name of the department in which the employee is working (department name exists in department table). Select emp_no, ename, dept.dname from emp.dept Where emp.deptno = dept.dept_no and emp_no = &emp_no. Emp_dept<--employee e.deptno=d.deptnoDEPT Result<-IIemp.enam,dname)emp_dept) The first operation in the joint operation will combine the tuples of the employee and department relations on the basis of the dept no.to form a relation called emp_dept. Then the PROJECT operation will create a relation RESULT with the attributes eno. Ename, and dname. To perform join between two relations, there should be a common field between them. Theta Join: A join condition is of the form <Condition>and<condition>and<condition> Where each condition is of the form Ai 0 Bj (dept.deptno = emp.dept_no). Ai is an attribute of R and Bj is an attribute of S. Ai and Bj have the same domain (same values) and 0 is one of the comparison operators (=,<,<=,>,>=,!=).

A join operation with such a general join condition is called a "Theta join". Equi Join: While joining if the comparison operator is = then it is equijoin. Eg. Select emp_no.ename.dept.dname from emp.dept. Where emp.deptno = dept.dept_no. Natural Join: It is denoted by symbol. The standard definition of natural join requires that the join attributes have the same name in both relations. In general, natural join is performed by equating all attribute pairs that have the same name in the two relations. The general format is: Here list l specifies list of attributes from R and list2 specifies a list of attributes from S.

Here, the joining is done over the attribute DNumber of Department relation and DNum of Project relation. In fact, DNum of Project is a foreign key which references DNumber of Department. Generally, in a natural join, the joining attribute is implicitly considered. Suppose the two relations have no attribute(s) in common, is simply the cross product of these two relations. Joining can be done between any set of attributes and need not be always with respect to the primary key and foreign key combinations. The expected size of the join result divided by maximum size i.e. join selectively. Outer join: It returns both matching and non matching rows. It differs from the inner join, in that the rows in one table having no matching rows in the other table will also appear in the results table, with nulls in the other attribute position, instead of being ignored as in the case with the inner join. It outputs rows even leads to a relation called

if they do not satisfy the join condition; the outer join operator is used with the table having n matching rows.

In the above example even though there is no matching row with B name, all workers are listed along with age and skill. If there is no match, simply get an empty skill column. The outer join can be used when we want to keep all the tuples in R or in S; those in both relations, whether or not they have matching tuples in the other relation. Left outer join: It is denoted by relation R in relation values. . The left outer join operation keeps every tuple in the first or left . If no matching tuple is found in S in the join, result is filled with null , and keeps every tuple in the second or right relation S in the

Right outer join: It is denoted by result of R

Full outer join: It is denoted by and keeps all tuples in both the left and right relations and when no matching tuples are found, filled with null values as needed. Division A division operation (denoted by ) is useful for a special kind of query; occasionally it may be used to solve certain kind of problems.

Consider the relations P (P) and Q (Q) as shown in the figure. The result of dividing P by Q is the relation R and it has two tuples. For each tuple in R, its product with the tuples of Q must be in P. In our example (a1, b1) must both be tuples in P: the same is true for (a5, b1) and (a5, b2) Examples of the division operations R = P + Q:

For e.g.: To retrieve the names of employees who work on all the projects that 'John Smith' works on. 1. Retrieve the list of project numbers that John Smith works on the intermediate relation SMITH_PNOS:

2. create a relation that includes a tuple < PNO,ESSN> whenever the employee whose social security number is ESSN works on the project whose number is PNO in the intermediate relation SSN_PNOS. SSN_PNOS < ESSN.PNO(WORKS_ON) 3. Apply the DIVISION operation to the two relations which gives the desired employees social security numbers.

TABLES

Notice here that 123,453 appear in SSN_PNOS in combination with all two tuples in SMITH_PNOS; that is why they appear in the resulting relation SSNS.

You might also like