DBMS Joins
DBMS Joins
TOPIC: JOINS
Overview
• Definition
• Types of Joins
• Example Queries
DEFINITION
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.
The inner join can be further divided into the following types:
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
Equi Join is an inner join that uses the equivalence condition for fetching the values
Query:
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.
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.
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
Output:
Query:
The Full-Outer(⟗) join contains all the values of both the tables
whether they have matching values in them or not.
FULL OUTER 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
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.
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
• 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.
• 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:
1 DELHI MANGALORE
2 MUMBAI DELHI