0% found this document useful (0 votes)
21 views

SQL Joins

SQL joins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SQL Joins

SQL joins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL JOINS

The SQL JOIN statement is used to combine rows from two tables based on a
common column and selects records that have matching values in these columns.
The main purpose of Join is to retrieve the data from multiple tables in other words
Join is used to perform multi-table queries.

SQL JOIN Syntax


SELECT columns_from_both_tables
FROM table1
JOIN table2
ON table1.column1 = table2.column2
Here table1 and table2 are the two tables that are to be joined and column1 is the
column in table1 that is related to column2 in table2
Example :

Here emp_dets can be joined with supervisor_dets based on the common column
supervisor-id to retrieve the supervisor data for a particular employee.
Types of JOINS in SQL
Depending on the users' needs, there are several types of joins. These joins are
broadly classified into four types,
 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL OUTER JOIN

SQL INNER JOIN


The SQL INNER JOIN statement joins two tables based on a common column and
selects rows that have matching values in these columns
SQL LEFT JOIN OR LEFT OUTER JOIN
The SQL LEFT JOIN combines two tables based on a common column. It then
selects records having matching values in these columns and the remaining rows
from the left table.
SQL RIGHT OUTER JOIN
SQL right outer join returns the matching rows of both tables along with the
unmatched rows from the right table. If a record from the right table does not have
any matched rows in the left table, it displays the record with NULL values.
A FULL OUTER JOIN returns the all the matching and unmatching rows from tables

IN DETAIL

SQL INNER JOIN


The SQL INNER JOIN statement joins two tables based on a common column and
selects rows that have matching values in these columns
Syntax :
SELECT column-name
FROM table-1 INNER JOIN table-2
WHERE table-1.column-name = table-2.column-name;

The inner join returns matching rows from both tables; therefore, it is also known as
Equi join. If we don’t specify the inner keyword, SQL performs the inner join
operation.
SQL LEFT JOIN OR LEFT OUTER JOIN
The SQL LEFT JOIN combines two tables based on a common column. It then
selects records having matching values in these columns and the remaining rows
from the left table.
In the below example, the left outer join returns the following rows:
Matched rows: Emp ID 1 and 2 exists in both the left and right tables.
Unmatched row: Emp ID 3 doesn’t exist on the right table. Therefore, we have a
NULL value in the query output.

Right outer join


SQL right outer join returns the matching rows of both tables along with the unmatched rows
from the right table. If a record from the right table does not have any matched rows in the
left table, it displays the record with NULL values.
A FULL OUTER JOIN returns the following rows in the output:
Matching rows between two tables.
Unmatched rows similar to left outer join: NULL values for unmatched rows from the
right table.
Unmatched rows similar to right outer join: Null values for unmatched rows from the
left table.

You might also like