CHAPTER -6
JOINS
Let’s make coding fun!
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
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both
tables
LEFT (OUTER) JOIN: Returns all records from the left table, and
the matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table,
and the matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table
CROSS Join combines every row from left table with every row in
the right table.
Also return and known as CARTISAN PRODUCT
• INNER
Joins compares two columns from two tables with equivalence
operator ‘=‘
Also known as EQUI JOINS and mostly used as default join
Use ON clause
• SELF
Joining a table to itself
Two rows from the same table combine to form a result row
Using FROM clause and alias
• OUTER
Joins to select all rows from the table on the left(or right or both)
regardless of whether the other table has values in common and
usually enter NULL where data is missing.
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values
in both tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all records from the left table
(table1), and the matched records from the right table (table2). The
result is NULL from the right side, if there is no match.
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all records from the right table
(table2), and the matched records from the left table (table1). The
result is NULL from the left side, when there is no match.
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Note: In some databases RIGHT JOIN is called RIGHT OUTER
JOIN.
SQL FULL OUTER JOIN Keyword
The FULL OUTER JOIN keyword return all records when there is a
match in either left (table1) or right (table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-
sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROkM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SQL Self JOIN
A self JOIN is a regular join, but the table is joined with itself.
Self JOIN Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.
CONCLUSION
In this chapter, we
have covered all about Joins and its importance.