Professional Documents
Culture Documents
16MIS0350
C.ARUN KRISHNA
1. Discuss the various types of inner join operations.
Why is theta join required?
There are three types of INNER join operation they are :
Equijoin
Natural join
Theta join
You may also perform EQUI JOIN by using JOIN keyword followed by ON
keyword and then specifying names of the columns along with their associated
tables to check equality.
Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
or
SELECT *
FROM table1
JOIN table2
[ON (join_condition)]
Here is an example of Equi Join in SQL.
To get agent name column from agents table and cust name and cust city
columns from customer table after joining said two tables with the following
condition -
1. working area of agents and customer city of customer table must be same,
SQL Code:
SELECT agents.agent_name,customer.cust_name,
customer.cust_city
FROM agents,customer
WHERE agents.working_area=customer.cust_city;
Output:
Equi join returns the matching column values of the associated tables. It uses a
comparison operator in the WHERE clause to refer equality.
2) Natural join
We have already learned that an EQUI JOIN performs a JOIN against equality
or matching column(s) values of the associated tables and an equal sign (=) is
used as comparison operator in the where clause to refer equality.
The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a
way that, columns with the same name of associated tables will appear once
only.
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Dont use ON clause in a natural join.
Syntax:
SELECT *
FROM table1
NATURAL JOIN table2;
Example:
To get all the unique columns from foods and company tables, the following
SQL statement can be used:
SQL Code:
SELECT *
FROM foods
NATURAL JOIN company;
3)Theta join
Theta join combines tuples from different relations provided they satisfy the
theta condition. The join condition is denoted by the symbol .
Notation
R1 R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn)
such that the attributes dont have anything in common, that is R1 R2 = .
Theta join can use all kinds of comparison operators.
Student
SID Name Std
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail
STUDENT Student.Std = Subject.Class SUBJECT
Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
Why is theta join required?
2)What role does the concept of foreign key play when specifying the most
common types of meaningful join operations?
We can join the two tables without the use of the foreign keys .if the data types
of the columns are of the same data type then two tables can be joined easily.
What if they are of not same data type
Even if there of not same data type we can join the two tables .
Then what is the role of foreigen keys ??
Here comes the typical question of how foreign keys play a important role
If we consider a table and there may be a same type of employee department
But in case of employee id it is notdifferent it is uique .
Primary key enforces uniqueness of values over one or more columns. Since ID
is not a primary key in Departments table, 2 or more departments may end up
having same ID value, which makes it impossible to distinguish between them
based on the ID column value.
Then we were not able to distinguish betwwen the coloumns if foregin keys
were not used .
Consider the following table
Id Name location
1 IT VELLORE
2 HR CHENNAI
3 PAYROLL TIRUPATHI