0% found this document useful (0 votes)
55 views

Joins: 1.inner Join

1. The document discusses different types of SQL joins including inner joins, outer joins, theta joins, and natural joins. 2. Inner joins return rows that satisfy the join condition from both tables. Outer joins return all rows from one table and matching rows from the other table. 3. Examples of theta, equi, natural, left, right, and full outer joins are provided with explanations and sample queries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Joins: 1.inner Join

1. The document discusses different types of SQL joins including inner joins, outer joins, theta joins, and natural joins. 2. Inner joins return rows that satisfy the join condition from both tables. Outer joins return all rows from one table and matching rows from the other table. 3. Examples of theta, equi, natural, left, right, and full outer joins are provided with explanations and sample queries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB 3

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.

Different Types of SQL JOINS-


1.Inner join
Equi join
Natural join
Theta join
2.Outer join
Left outer join
Right outer join
Full outer join
Table 1-
create table emp(empid int(5),empname char(20),city char(20),department char(20));
insert into emp values(1,'akshara','seoul','cybersecurity');
insert into emp values(2,'kumoulica','busan','testing');
insert into emp values(3,'satwika','kanigiri','developing');
insert into emp values(4,'bhagath','canada','programmer');
insert into emp values(5,'rose','austrailla','leader');
insert into emp values(6,'woobin','vijayawada','designing');
select * from emp;

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-

FULL OUTER JOIN-


Purpose-
keyword returns all records when there is a match in left (table1) or
right (table2) table records.

Syntax-
SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name WHERE condition;

You might also like