Join Operation
Join Operation
OPERATI
ON
~AND IT’S TYPES IN SQL
INTRODUCTIO
N: JOIN
OPERATION
The join operation in SQL combines rows
from two or more tables based on a
related column. It allows you to view data
from multiple tables as a single result by
matching rows with common values (often
keys) in specifi ed columns. Joins are
essential for integrating data across
tables, making it easier to query and
analyze related information in a relational
database.
INNER JOIN
An inner join in SQL combines
rows from two tables and
returns only those rows where
there is a match between the
specifi ed columns in both
tables. If there is no match,
the rows are excluded from the
result set. This join type is
commonly used to retrieve
related data from multiple
tables, ensuring only relevant,
interconnected records are
included in the output.
EMPLOYE DEPARTMENT
E TABLE TABLE
OUTPUT:-
SELECT EMPLOYEES.NAME,
DEPARTMENTS.DEPARTMENTNAME
FROM EMPLOYEES
INNER JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENTID =
DEPARTMENTS.DEPARTMENTID;
CONDITIONAL JOIN
A conditional join in SQL is a join that
uses specific conditions to match rows
between tables. Rather than relying
solely on equal values between
columns, it allows for more complex
conditions, such as using inequalities
(>, <) or multiple conditions combined
with AND/OR. Conditional joins are
helpful when you need more flexibility in
defining how rows from each table
should be paired in the results.
ORDER SHIPMENT
TABLE TABLE
OUTPUT:-
SELECT
O.ORDERID,
O.CUSTOMERID,
O.ORDERDATE,
S.SHIPMENTID,
S.SHIPMENTDATE
FROM
ORDERS O
JOIN
SHIPMENTS S
ON
O.ORDERID = S.ORDERID
AND
S.SHIPMENTDATE > O.ORDERDATE;
EQUI JOIN
An equi join in SQL is a join operation
that combines rows from two or more
tables based on an equality condition
between specified columns. It returns
only those rows where the values in the
specified columns are equal in each
table. Equi joins are commonly used to
link related data across tables with
matching values, providing a
straightforward way to retrieve
interconnected records.
EMPLOYE DEPARTMENT
ES TABLE TABLE
OUTPUT:-
SELECT EMPLOYEES.NAME,
DEPARTMENTS.DEPARTMENTNAME
FROM EMPLOYEES
JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENTID =
DEPARTMENTS.DEPARTMENTID;
OUTER JOIN
An outer join in SQL returns all rows
from one or both tables, including rows
that do not have a match in the other
table. For unmatched rows, it fills in
NULL values for the missing data. Outer
joins are useful when you want to retain
all records from one or both tables,
regardless of whether they meet the
join condition.
Left Outer Join
TYPE
records from the right table. If there is no match,
NULL values will appear for columns from the right
table.
• Use Case: Useful when you want to see all
S OF
entries from one table and any related data from
another.
Right Outer Join
OUTE
(the second table specified), and the matched records
from the left table. If there is no match, NULL values
will appear for columns from the left table.
• Use Case: Useful when you want to ensure all entries
from the right table are included, even if there are no
R
corresponding entries in the left table.
Full Outer Join
JOIN
matched records; when there are no matches,
it fills in NULL values for the missing data from
either table.
• Use Case: Useful for getting a complete view
of both tables, including all data regardless of
matches.
LEFT OUTER
JOIN
EMPLOYE DEPARTMENT
E TABLE TABLE
OUTPUT:-
SELECT EMPLOYEES.NAME,
DEPARTMENTS.DEPARTMENTNAME
FROM EMPLOYEES
LEFT JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENTID =
DEPARTMENTS.DEPARTMENTID;
RIGHT
OUTER JOIN
EMPLOYE DEPARTMENT
E TABLE TABLE
OUTPUT:-
SELECT EMPLOYEES.NAME,
DEPARTMENTS.DEPARTMENTNAME
FROM EMPLOYEES
RIGHT JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENTID =
DEPARTMENTS.DEPARTMENTID;
FULL OUTER
JOIN
EMPLOYE DEPARTMENT
E TABLE TABLE
OUTPUT:-
SELECT EMPLOYEES.NAME,
DEPARTMENTS.DEPARTMENTNAME
FROM EMPLOYEES
FULL OUTER JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENTID =
DEPARTMENTS.DEPARTMENTID;
NATURAL JOIN
A natural join is a type of join operation
in SQL that automatically matches
columns between two tables based on
common column names and combines
the rows with matching values in these
columns. Unlike other joins, a natural
join does not require specifying the
columns to join on because it assumes
that both tables share at least one
column with the same name and
compatible data types.
NATURAL
JOIN
CUSTOM PRODUCTS
ER TABLE
TABLE
OUTPUT:-
SELECT *
FROM CUSTOMER
NATURAL JOIN PRODUCT;
THANK
YOU
VERY
MUCH!