TRAINING CLASSES ON MYSQL
DEPARTMENT OF DS-CS & CSIT
TOPIC: JOINS
Overview
• Definition
• Types of Joins
• Example Queries
DEFINITION
• In DBMS, a join is mainly used to combine two tables based on a
specified common field between them.
• Join operation is one of the most useful operations in relational
algebra and the most commonly used way to combine information
from two or more relations.
TYPES OF JOINS
1. Inner Join
2. Outer Join
3. Self Join
4. Cross join
INNER JOIN
• Inner Join is a join that can be used to return all the values that
have matching values in both the tables
• In an inner join, only those tuples that satisfy the matching criteria
are included, while the rest are excluded.
• Inner Join can be depicted using the below diagram.
CONTD..
The inner join can be further divided into the following types:
a) Condition or Theta join
b) Equi Join
c) Natural Join
CONDITION/THETA JOIN
This join can use any condition in selection criteria. The general case of JOIN operation
Ex: A ⋈θ B
is called a Theta join. It is denoted by symbol θ.
RESERVES R1 SAILORS S1
SID BID DAY SID SNAME RATING AGE
22 101 10/10/96 22 DUSTIN 7 45
58 103 11/12/96 31 LUBBER 8 55
58 RUSTY 10 35
CONDITION/THETA JOIN
The result of S1 ⋈S1.sid< R1.sid R1
SID SNAME RATING AGE SID BID DAY
22 DUSTIN 7 45 58 103 11/12/96
31 LUBBER 8 35 58 103 11/12/96
EQUI JOIN
Equi Join is an inner join that uses the equivalence condition for fetching the values
Ex: A ⋈ A . column = B . column B
of two tables.
Consider the following employee and department tables:
empid empname deptid deptid deptname
1 HARRY 2 1 CSE
2 TOM 3 2 MECH
3 JOY 5 3 IT
4 ROY 8
EQUI JOIN
Query:
Select employee.empid, employee.empname, department.deptname from employee Inner
Join department on employee.deptid = department.deptid;
empid empname deptname
1 HARRY MECH
2 TOM IT
Note: In Equi join the common column name can be same or different.
NATURAL JOIN
Natural Join is an inner join that returns the values of the two tables on the basis of a
common attribute that has the same name and domain.
Note: In Natural join, the tables should have same column names to perform equality
operations on them.
Query: select * from employee natural join department;
If we want to specify attribute names, the query will be:
Select employee.empid,employee.empname,department.deptid,department.deptname
from employee natural join department;
OUTER JOIN
Outer Join is a join that can be used to return the records in both the tables
whether it has matching records in both the tables or not.
In an outer join, along with tuples that satisfy the matching criteria, we also
include some or all tuples that do not match the criteria.
The outer join can be further divided into three types:
1. Left-Outer Join
2. Right-Outer Join
3. Full-Outer Join
LEFT OUTER JOIN
The Left-Outer Join (⟕) is an outer join that returns all the
values of the left table, and the values of the right table
that has matching values in the left table.
LEFT OUTER JOIN
Query: Select employee.empId, employee.empName, department.deptName from
employee Left Outer Join department on employee.deptId = department.deptId;
Output:
empid empname deptname
1 HARRY MECH
2 TOM IT
3 JOY NULL
4 ROY NULL
RIGHT OUTER JOIN
The Right-Outer Join (⟖) is an outer join that returns all
the values of the right table, and the values of the left
table that has matching values in the right table.
RIGHT OUTER JOIN
Query:
Select employee.empId, employee.empName, department.deptName from
employee Right Outer Join department on employee.deptId = department.deptId;
empid empname deptname
NULL NULL CSE
1 HARRY MECH
2 TOM IT
FULL OUTER JOIN
The Full-Outer(⟗) join contains all the values of both the tables
whether they have matching values in them or not.
FULL OUTER JOIN
Query: empid Empname deptid Deptid deptname
1 HARRY 2 1 CSE
Select * from 1 HARRY 2 2 MECH
employee Full Join 1 HARRY 2 3 IT
department;
2 TOM 3 1 CSE
Output: 2 TOM 3 2 MECH
2 TOM 3 3 IT
3 JOY 5 1 CSE
3 JOY 5 2 MECH
3 JOY 5 3 IT
4 ROY 8 1 CSE
4 ROY 8 2 MECH
4 ROY 8 3 IT
SELF JOIN
• The self join allows you to join a table to itself.
• This implies that each row of the table is combined with every other row of the
table.
• The self join can be viewed as a join of two copies of the same table.
• The table is not actually copied, but SQL performs the command as though it were.
This is accomplished by using table name aliases to give each instance of the table a
separate name.
• It is most useful for extracting hierarchical data or comparing rows with in the same
table
SELF JOIN
create table customers(firstname char(20),country char(20))
insert into customers values('nav','India')
insert into customers values('sav','India')
insert into customers values('nav','us')
insert into customers values('nav','India’)
select * from customers
select c1.firstname as firstperson,c2.firstname as secondperson,c1.country
from customers c1,customers c2
where c1.country=c2.country and c1.firstname!=c2.firstname;
select c1.firstname as firstperson,c2.firstname as secondperson,c1.country
from customers c1 JOIN customers c2
on c1.country=c2.country and c1.firstname!=c2.firstname;
CROSS JOIN
• The cross join command in SQL, also known as a cartesian join, returns all
combinations of rows from each table.
• Note, that this join does not need any condition to join two tables.
• In fact, CROSS JOIN joins every row from the first table with every row from
the second table and its result comprises all combinations of records in two
tables.
CROSS JOIN
CREATE TABLE Meals(MealName VARCHAR(100))
CREATE TABLE Drinks(DrinkName VARCHAR(100))
INSERT INTO Drinks VALUES('Orange Juice'), ('Tea'), ('Cofee')
INSERT INTO Meals VALUES('Omlet'), ('Fried Egg'), ('Sausage’)
SELECT * FROM Meals;
SELECT * FROM Drinks
SELECT * FROM Meals CROSS JOIN Drinks
DIFFERENCE BETWEEN CROSS JOIN & FULL OUTER JOIN
A Cross Join combines every row from one table with every row from another,
creating a Cartesian product, while a Full outer join returns all rows from both
tables, including unmatched rows, based on a specified condition.
Feature CROSS JOIN FULL OUTER JOIN
Purpose Generate all possible Return all rows from both
combinations (Cartesian tables, including matched and
product) unmatched rows
ON Clause No ON clause required Requires an ON clause (join
condition)
Result All combinations of rows All rows from both tables, with
matched and unmatched rows
Use Cases Combinatorial data generation Comprehensive data analysis,
including unmatched rows from
either table
USING CLAUSE
When the columns used to join are called equally in both tables, the USING
clause can be used as a shorthand.
For example, if the tables COURSES and TECHNOLOGIES have a common
column named ‘technology_id’, you can use the following query
SELECT * FROM COURSES
JOIN TECHNOLOGIES
USING (technology_id);
CTE(COMMON TABLE EXPRESSION)
• A Common Table Expression (CTE) is the result set of a query which exists temporarily
and for use only within the context of a larger query. Much like a derived table, the
result of a CTE is not stored and exists only for the duration of the query.
• In SQL, both CTEs (Common Table Expressions) and subqueries are
used to create temporary result sets within a query, but CTEs are named
and can be referenced multiple times, while subqueries are typically used for a single,
nested operation.
• CTEs make complex queries easier to write and read by breaking them
into simpler parts.
QUERY 1
Input: FLIGHTS
Cust_id Flight_id Origin Destination
1 SG1234 DELHI HYDERABAD
1 SG3476 KOCHI MANGALORE
1 69876 HYDERABAD KOCHI
2 68749 MUMBAI VARANASI
2 SG5723 VARANASI DELHI
Expected output:
Cust_id Origin Destination
1 DELHI MANGALORE
2 MUMBAI DELHI