SQL Joins with Example - Inner, Left, Right, Full

.
 SQL Joins (Inner Joins, Left Joins, Right Joins, Full Joins)- Joins in SQL With Example

In this tutorial, we are going to learn joins in SQL with examples and join types. 

Joins in SQL

SQL Joins clause is used to combine data or rows from two or more tables, based on a related column between them.
"To retrieve the data from multiple tables we used Joins"

Syntax of Joins 
The basic syntax of joins is given below.


SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;


Types of SQL Joins
Different types of joins are as follows
  • SQL INNER JOIN (Simple Join) 
  • SQL LEFT OUTER JOIN (LEFT JOIN) 
  • SQL RIGHT OUTER JOIN (RIGHT JOIN) 
  • SQL FULL OUTER JOIN (FULL JOIN)

SQL INNER JOIN (Simple Join) 
The INNER JOIN keyword selects records that have matching values in both tables.

Syntax of Inner Join
The syntax of the inner join is given below.

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

Example of Inner Joins 


SQL LEFT OUTER JOIN (LEFT JOIN)

In SQL, a LEFT JOIN is used to combine rows from two or more tables based on a related column between them. The result of a LEFT JOIN includes all the rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for the columns of the right table.

Syntax of Left Outer Join 

The basic syntax of a LEFT OUTER JOIN is as follows:

SELECT column_list
FROM table1
LEFT JOIN table2 ON join_condition;


Example of Left Outer Join


SQL RIGHT OUTER JOIN (RIGHT JOIN)

In SQL, a RIGHT JOIN or RIGHT OUTER JOIN is used to combine rows from two or more tables based on a related column between them. The result of a RIGHT JOIN includes all the rows from the right table and the matching rows from the left table. If there is no match, NULL values are returned for the columns of the left table.

Syntax of Right Outer Join 

The basic syntax of a RIGHT OUTER  JOIN is as follows:

SELECT column_list
FROM table1
RIGHT JOIN table2 ON join_condition;

Example of Right Outer Join 


SQL FULL OUTER JOIN (FULL JOIN)

In SQL, a FULL OUTER JOIN is used to combine rows from two or more tables based on a related column between them. The result of a FULL OUTER JOIN includes all the rows from both the left and right tables, matching rows are included once, and if there is no match, NULL values are returned for the columns of the non-matching table.

Syntax of Full Outer Join 

The basic syntax of a Full OUTER  JOIN is as follows:
SELECT column_list
FROM table1
FULL OUTER JOIN table2 ON join_condition;
Example of Full Outer Joins

Hope!!! The above Tutorial on SQL Joins With Examples is helpful for you...
      
Team,
QA acharya                                    


Joins In SQL With Example

Post a Comment

0 Comments