You are on page 1of 7

DIGITAL ASSIGNMENT-2

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

So here we can discuss one by one


1) Equijoin
For whatever JOIN type (INNER, OUTER, etc), if we use ONLY the equality
operator (=), then we say that the JOIN is an EQUI JOIN.
SQL EQUI JOIN performs a JOIN against equality or matching column(s)
values of the associated tables. An equal sign (=) is used as comparison operator
in the where clause to refer equality.

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.

Sample table: agents


Sample table: customer

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,

the following SQL statement can be used:

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.

Natural Join: Guidelines

- 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:

Here is an example of SQL natural join between two tables:


ITEM_ID ITEM_NAME ITEM_UNIT COMPANY_ID
+---------+--------------+-----------+------------+
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |

Sample table: company

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?

A theta join allows for arbitrary comparison relationships (such as


An equijoin is a theta join using the equality operator.
A natural join is an equijoin on attributes that have the same name in
each relationship.

In a relational database, a join is just an expression involving more than one


table and formulas between the rows of those tables that restrict the result set.
Arbitrary comparison between single columns of different tables are one kind of
possibility of those formulas. This is nothing special to be "required" in the
dbms. All formulas are allowed, some joins have them, others don't, depending
on what real life problem is going to be solved.
Theta Join, Equijoin, and Natural Join are called inner joins. ... Therefore, we
need to use outer joins to include all the tuples from the participating relations
in the resulting relation. There are three kinds of outer joins left outerjoin,
right outer join, and full outer join.
Theta join
This is same as EQUI JOIN but it allows all other operators like >, <, >= etc.
Many consider both EQUI JOIN and Theta JOIN similar
to INNER, OUTER etc JOINs. But I strongly believe that its a mistake and makes
the ideas vague. Because INNER JOIN, OUTER JOIN etc are all connected with
the tables and their data where as EQUI JOIN and THETA JOIN are only
connected with the operators we use in the former.
Again, there are many who consider NATURAL JOIN as some sort of
"peculiar" EQUI JOIN. In fact, it is true, because of the first condition I
mentioned for NATURAL JOIN. However, we dont have to restrict that simply
to NATURAL JOINs alone. INNER JOINs, OUTER JOINs etc could be an EQUI
JOIN too.
Beyond all the other types of join operations theta join allows operators
like >,<,>= etc
Similar to the equijoin it allows all other stuff.

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

And if we consider an another table


ID NAME GENDER SALARY DEPARTMENT
1 Manoj Male 2000 3
2 Prabhavathy Female 60000 1
mamm
3 Narayana Male 5000 2

If we want to join two tables mentioned above


If the foreign keys were not used then if we want to insert a person or want to
join a new person into the second mentioned table ,if that person has the
department number as 100 we cant insert that particular person. Since the id of
the first table doesnt hold the id number of 100
If a foreign key is used then we can easily connect or join two tables
Or
If we want to update the second table of narayana_deparment number to 100 we
cant if we doesnt use a foreign key .since the table one which is supposed to be
join has no id number of 100
If we even try to update then the total data of the particular employee will be
lost .
Without foreign keys we will lose the referential integrity.
Foreign key enforces referential integrity. Without foreign key constraint on
Department ID column in Employees table, it is possible to insert a row into
Employees table with a value for Department ID column that does not exist in
Departments table.
So these is the main role of the foreign keys in the case of all the major join
operations.

You might also like