SQL (Join)
In SQL , a query that can fetch attributes(column) from two or more than two tables
based on condition is called Join.
Syntax for joining:
Select <fn1>,<fn2>…. from table1,table2
< where table1.identical column=table2.identical column> [ and <condition>]
Consider these two tables:
Customer Item
CID Nam INO INO IName
e A01 Soap
1 Aa A01 A02 Talc. Powder
2 Bb A02
3 Cc A01
4 Dd A02
1) Display Customer name and Item Name from the tables.
Sol: select Name,IName from Customer,Item where Customer.INO=Item.INO;
2) Display Customer name and Item Name for those who bought ‘Talc. Powder’.
Sol: select Name,IName from Customer,Item where Customer.INO=Item.INO and INAME=’Talc. Powder’;
3) Display Customer name for those who bought ‘Soap’.{ here data is to be retrieved from one table but the
condition data is present in other table so it is a query of Join}
Both table names is must as it is a query of join
Sol: select Name from Customer,Item where Customer.INO=Item.INO and INAME=’Soap’;
4)Display Customer ID,Item No. and Item Name for all the customers.
Sol: select CID,Name,Item.INO from Customer,Item where Customer.INO=Item.INO;
One of the table name is compulsory to mention to specify, as this column is present in both the table
Table alias: It is a temporary label (duplicate name) given to table in from clause.
E.g. : We can write the query 1(given above) in the following manner also:
select Name,IName from Customer C, Item I where C.INO=I.INO;
C and I are the table alias, and they can be used instead of table name(as they are duplicate table name only)
Natural Join and Equi Join
1) Natural join is a implicit join Natural Join joins two tables based on same attribute
name and datatypes. The resulting table will contain all the attributes of both the
table but keep only one copy of each common column. if column name is not
same in both the table Cartesian product will be done in between two table
2) Equi join in SQL is a type of Inner Join in which two or more tables are joined by
equating the common column values of the tables. The table participating in Equi
join should have at least one common column on which equality operation can be
performed. i.e., to compare identical column we have to use only equality
operator (=).
Cartesian product
Cartesian product is also known as CROSS JOIN denoted by (AxB)
In Cartesian product all the rows present in the first table multiplied by
all rows present in the second table. It is similar to the Inner Join,
where the join condition is not available with this clause.
Cartesian product yields a new relation which has a degree equal to
the sum of the degree of two relation and tuple of the new relation is
product of tuple of both the relation
Differnce between eqijoin and natural join
equijoin natural join
In Equi join, the common column name can be In Natural join, the tables should have the same
the same or different. column names to perform equality operations on
them.
equi join has the column name of both the tables natural join only has one of the column names in
in the resulting table the resulting table.
SELECT * FROM table1,table2 where SELECT * FROM table1 NATURAL JOIN table2
table1.id = table2.id