Module 3 Joins
Module 3 Joins
subqueries, single row subquery, multiple row subquery, Top N Analysis, DML
Statements insert, update, delete. Merge. Oracle data types, TCL Command:
commit, rollback and savepoint Set Operators: Union, intersect & Minus Clause
JOIN: In relational databases, join refers to an operation that combines rows from
two or more tables based on a related column between them. Joins allow querying
across tables to retrieve data that’s connected.
The join clause allows us to retrieve data from two or more related tables into a
meaningful result set. We can join the table using a SELECT statement and a join
condition. It indicates how SQL Server can use data from one table to select rows
from another table. In general, tables are related to each other using foreign
key constraints.
INNER JOIN
This JOIN returns all records from multiple tables that satisfy the specified join
condition. It is the simple and most popular form of join and assumes as
a default join. If we omit the INNER keyword with the JOIN query, we will get
the same output.
The following visual representation explains how INNER JOIN returns the
matching records from table1 and table2:
INNER JOIN
This type of SQL server JOIN returns rows from all tables in which the join condition is
true. It takes the following syntax:
Table: Student
Table: Fee
SELF JOIN
A table is joined to itself using the SELF JOIN. It means that each table row is
combined with itself
The SELF JOIN can be thought of as a JOIN of two copies of the same tables. We can
do this with the help of table name aliases to assign a specific name to each table's
instance
It's a useful way to extract hierarchical data and comparing rows inside a single table.
Example
CROSS JOIN
CROSS JOIN in SQL Server combines all of the possibilities of two or more tables
and returns a result that includes every row from all contributing tables. It's also
known as CARTESIAN JOIN because it produces the Cartesian product of all
linked tables. The Cartesian product represents all rows present in the first table
multiplied by all rows present in the second table.
The below visual representation illustrates the CROSS JOIN. It will give all the
records from table1 and table2 where each row is the combination of rows of
both tables:
1. SELECT column_lists
2. FROM table1
3. CROSS JOIN table2;
Example
OUTER JOIN
OUTER JOIN in SQL Server returns all records from both tables that satisfy
the join condition. In other words, this join will not return only the matching
record but also return all unmatched rows from one or both tables.
The following syntax illustrates the use of LEFT OUTER JOIN in SQL Server:
1. SELECT column_lists
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON table1.column = table2.column;
Example
We can demonstrate the LEFT OUTER JOIN using the following command:
This output shows that the unmatched row's values are replaced with NULLs in
the respective columns.
The following syntax illustrates the use of RIGHT OUTER JOIN in SQL Server:
1. SELECT column_lists
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON table1.column = table2.column;
Example
The following example explains how to use the RIGHT OUTER JOIN to get
records from both tables:
In this output, we can see that no column has NULL values because all rows in
the Fee table are available in the Student table based on the specified condition.
The following syntax illustrates the use of FULL OUTER JOIN in SQL Server:
1. SELECT column_lists
2. FROM table1
3. FULL [OUTER] JOIN table2
4. ON table1.column = table2.column;
Example
The following example explains how to use the FULL OUTER JOIN to get records
from both tables:
• Use table prefixes to qualify column names that are in multiple tables.
• Distinguish columns that have identical names but reside in different tables by using
column aliases.
• The join condition for the natural join is basically an equijoin of all columns with the
same name.