Joins: 1.inner Join
Joins: 1.inner Join
Date-20-09-2021
JOINS
A JOIN clause is used to combine rows from two or more
tables, based on a related column between them.
Table 2-
create table info(empid int(5),age int(10),projectno int(5));
insert into info values(1,22,101);
insert into info values(2,19,102);
insert into info values(3,20,103);
insert into info values(4,21,104);
insert into info values(7,28,105);
select * from info;
Inner join
Inner join is used to return rows from both tables which
satisfy the given condition. It is the most widely used join operation
and can be considered as a default join-type.
THETA JOIN-
Purpose-
THETA JOIN allows you to merge two tables based on the condition represented
by theta. Theta joins work for all comparison operators. It is denoted by symbol θ.
The general case of JOIN operation is called a Theta join.
Syntax-
SELECT column_name ,column_name from table1 JOIN
table2 ON column_name(condition < or >);
Query-
select age,projectno from info join emp on age<20;
Output-
EQUI JOIN-
Purpose-
EQUI JOIN is done when a Theta join uses only the equivalence condition.
Syntax-
SELECT column_name ,column_name from table1 JOIN
table2 ON column_name=condition;
Query-
select age,projectno from info join emp on age=20;
Output-
NATURAL JOIN-
Purpose-
NATURAL JOIN does not utilize any of the comparison operators.
In this type of join, the attributes should have the same name and
domain. In Natural Join, there should be at least one common attribute
between two relations.
Syntax-
Select * from table1 NATURAL JOIN table2;
Query-
select * from emp natural join info;
Output-
Table 1
create table customers(customer_id int(5) , customer_name char(10) , city
char(10) , country char(20));
insert into customers values(1,'kumoulica','berlin','germany');
insert into customers values(2,'akshara','hyderabad','india');
insert into customers values(3,'kanna','mexicodf','mexico');
select * from customers;
Table 2
create table orders (orderid int(5) , customerid int(5) , employeeid int(5) ,
shipperid int(5));
insert into orders values(10308,2,7,3) ;
insert into orders values(10309,37,3,1);
insert into orders values(10310,77,8,2);
select * from orders;
Outer join-
An outer join is used to return results by
combining rows from two or more tables
LEFT OUTER JOIN-
Purpose-
Returns all records from the left table (table1), and the matching
records from the right table (table2).
Syntax-
SELECT column_name(s) FROM table1 LEFT JOIN table2
ON table1.column_name = table2.column_name;
Query-
select customers.customer_name,orders.orderid from customers left join orders
on customers.customer_id=orders.customerid order by
customers.customer_name;
Output-
RIGHT OUTER JOIN-
Purpose-
returns all records from the right table (table2), and the matching records
from the left table (table1).
Syntax-
SELECT column_name(s) FROM table1 RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Query-
select customers.customer_name,orders.orderid from customers right
join orders on customers.customer_id=orders.customerid order by
customers.customer_name;
Output-
Syntax-
SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name WHERE condition;