Understanding SQL JOINs: A Comprehensive Guide with Examples and Syntaxes

SQL JOINs are essential for combining records from two or more tables in a relational database. Understanding JOINs is crucial for effective database querying and data manipulation. In this blog, we will explore the different types of SQL JOINs, provide valid examples, and explain their syntaxes.

Are you looking for Live Training in C#?
The upcoming batch for C#.NET is sceduled.
Book Your Seat Now.
Types of SQL JOINs
- INNER JOIN
- LEFT JOIN (LEFT OUTER JOIN)
- RIGHT JOIN (RIGHT OUTER JOIN)
- FULL JOIN (FULL OUTER JOIN)
- CROSS JOIN
INNER JOIN
The INNER JOIN
keyword selects records that have matching values in both tables.
Syntax
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example
Consider two tables, Employees
and Departments
.

INNER JOIN Query
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result

LEFT JOIN (LEFT OUTER JOIN)
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.
Syntax
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example
LEFT JOIN Query
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result

RIGHT JOIN (RIGHT OUTER JOIN)
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 if there is no match.
Syntax
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example
RIGHT JOIN Query
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result


Are you looking for Live Training in C#?
The upcoming batch for C#.NET is sceduled.
Book Your Seat Now.
FULL JOIN (FULL OUTER JOIN)
The FULL JOIN
keyword returns all records when there is a match in either left (table1) or right (table2) table records. The result is NULL from either side if there is no match.
Syntax
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
Example
FULL JOIN Query
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result

CROSS JOIN
The CROSS JOIN
keyword returns the Cartesian product of the two tables. It combines each row from the first table with each row from the second table.
Syntax
SELECT columns
FROM table1
CROSS JOIN table2;
Example
CROSS JOIN Query
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Result

Example Code
Here is an example using a database client like SQL Server Management Studio or MySQL Workbench:
-- INNER JOIN Example
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
-- LEFT JOIN Example
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
-- RIGHT JOIN Example
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
-- FULL JOIN Example
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
-- CROSS JOIN Example
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Conclusion
Understanding SQL JOINs is fundamental for working with relational databases. Each type of JOIN serves a specific purpose and is used based on the requirements of the query. By mastering INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and CROSS JOIN, you can efficiently combine and manipulate data from multiple tables.
By following this guide, you should now have a clear understanding of the various types of SQL JOINs, their syntax, and their use cases.

Are you looking for Live Training in C#?
The upcoming batch for C#.NET is sceduled.
Book Your Seat Now.