0% found this document useful (0 votes)
105 views8 pages

Understanding SQL Join Types

Uploaded by

roshanjadhav2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views8 pages

Understanding SQL Join Types

Uploaded by

roshanjadhav2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter : 4

Structured Query Language (SQL)


-----------------------------------------------------------------------------------------------------------------------------------------

SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to combine
two or more tables".

The SQL JOIN clause takes records from two or more tables in a database and combines it together.

JOIN Keyword is used in SQL queries for joining two or more tables. Minimum required condition for
joining table, is (n-1) where n, is number of tables. A table can also join to itself, which is known as,
Self Join.

Why SQL JOIN is used?


If you want to access more than one table through a select statement.

If you want to combine two or more table then SQL JOIN statement is used .it combines rows of that
tables in one table and one can retrieve the information by a SELECT statement.

The joining of two or more tables is based on common field between them.

SQL INNER JOIN also known as simple join is the most common type of join.

The various SQL join types are as follows


1) SQL inner join
2) SQL outer join
a) SQL left join or left outer join
b) SQL right join or right outer join
c) SQL full join or full outer join
3) SQL self join
4) SQL cross join

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------

SQL inner join


The simplest and most common form of a join is the SQL inner join the default of the SQL join
types used in most database management systems. It’s the default SQL join you get when you
use the join keyword by itself.
The INNER JOIN keyword selects all rows from both the tables as long as the condition
satisfies. This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be same.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------

Student

StudentCourse

SELECT StudentCourse.COURSE_ID, [Link], [Link] FROM Student


INNER JOIN StudentCourse

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
ON Student.ROLL_NO = StudentCourse.ROLL_NO;

SQL outer join

On joining tables with a SQL inner join, the output returns only matching rows from both the tables. When
using a SQL outer join, not only it will list the matching rows, it will also list the unmatched rows from the
other tables.

SQL left join or left outer join


A SQL left outer join will return all the records from the left table in the join clause, regardless of matching records in
the right table.

The left SQL outer join includes rows where the condition is met plus all the rows from the table on the left where the
condition is not met. Fields from the right table with no match will be displayed as null values.

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
SELECT
[Link],StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO =
Student.ROLL_NO;

SQL right join or right outer join:

RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right side of the
join and matching rows for the table on the left side of join. The rows for which there is no matching row
on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN
.Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are same.

SELECT [Link],StudentCourse.COURSE_ID

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
FROM Student

RIGHT JOIN StudentCourse

ON StudentCourse.ROLL_NO = Student.ROLL_NO;

SQL full join or full outer join


FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both the tables.
The rows for which there is no matching, the result-set will contain NULL values.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

SELECT [Link],StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
SQL self join
As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the table is joined
with itself and all other rows depending on some conditions.
In other words we can say that it is a join between two copies of the same table.

SELECT a.coulmn1 , b.column2


FROM table_name a, table_name b
WHERE some_condition;

table_name: Name of the table.


some_condition: Condition for selecting the rows.

SELECT a.ROLL_NO , [Link]


FROM Student a, Student b
WHERE a.ROLL_NO < b.ROLL_NO;

SQL cross join / CARTESIAN JOIN:


The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there is a join
for each row of one table to every row of another table. This usually happens when the
matching column or WHERE condition is not specified.
● In the absence of a WHERE condition the CARTESIAN JOIN will behave like a
CARTESIAN PRODUCT . i.e., the number of rows in the result-set is the product of
the number of rows of the two tables.
● In the presence of WHERE condition this JOIN will function like a INNER JOIN.
● Generally speaking, Cross join is similar to an inner join where the join-condition will
always evaluate to True

SELECT table1.column1 , table1.column2, table2.column1...


FROM table1
CROSS JOIN table2;

table1: First table.


table2: Second table

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
In the below query we will select NAME and Age from Student table and COURSE_ID from
StudentCourse table. In the output you can see that each row of the table Student is joined with
every row of the table StudentCourse.
The total rows in the result-set = 4 * 4 = 16.

SELECT [Link], [Link], StudentCourse.COURSE_ID


FROM Student
CROSS JOIN StudentCourse;

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV [Link] Mangore

You might also like