riven

Riven

Riven

SQL JOIN operation

A join is a SQL operation used to combine rows from two or more tables based on a related column between them. Joins are essential for querying data in relational databases, allowing you to extract meaningful information by correlating data stored in different tables.

Types of Joins

SQL provides several types of joins, each serving a different purpose:

  1. INNER JOIN
  2. LEFT JOIN (or LEFT OUTER JOIN)
  3. RIGHT JOIN (or RIGHT OUTER JOIN)
  4. FULL JOIN (or FULL OUTER JOIN)
  5. CROSS JOIN
  6. SELF JOIN

Let’s explore each type in detail with examples.

1. INNER JOIN

INNER JOIN returns only the rows that have matching values in both tables.

Syntax

SQL JOINS

Example

Consider two tables: Employees and Departments.

				
					CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(100),
    DepartmentID INT
);

CREATE TABLE Departments (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(100)
);
				
			

Sample Data

				
					INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID) VALUES
(1, 'Alice', 1),
(2, 'Bob', 2),
(3, 'Charlie', 1);

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES
(1, 'HR'),
(2, 'Engineering');
				
			

INNER JOIN Query

To get a list of employees along with their department names:

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

Result:

EmployeeNameDepartmentName
AliceHR
CharlieHR
BobEngineering

2. LEFT JOIN (LEFT OUTER JOIN)

LEFT JOIN returns all the rows from the left table and the matched rows from the right table. If there’s no match, NULL values are returned for columns from the right table.

Syntax

				
					SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
				
			

Example

Continuing from the previous example, let’s say we want to list all employees and their departments, including employees who might not belong to any department.

				
					SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
				
			

Result:

EmployeeNameDepartmentName
AliceHR
BobEngineering
CharlieHR

If we add an employee without a department:

				
					INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID) VALUES
(4, 'David', NULL);
				
			

Re-running the LEFT JOIN query:

Updated Result:

EmployeeNameDepartmentName
AliceHR
BobEngineering
CharlieHR
DavidNULL

3. RIGHT JOIN (RIGHT OUTER JOIN)

RIGHT JOIN returns all the rows from the right table and the matched rows from the left table. If there’s no match, NULL values are returned for columns from the left table.

Syntax

				
					SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
				
			

Example

Let’s retrieve a list of all departments and any employees assigned to them:

				
					SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
				
			

Result:

EmployeeNameDepartmentName
AliceHR
CharlieHR
BobEngineering
NULLEngineering

If we add a new department with no employees:

				
					INSERT INTO Departments (DepartmentID, DepartmentName) VALUES
(3, 'Marketing');
				
			

Re-running the RIGHT JOIN query:

Updated Result:

EmployeeNameDepartmentName
AliceHR
CharlieHR
BobEngineering
NULLMarketing

4. FULL JOIN (FULL OUTER JOIN)

FULL JOIN returns all rows from both tables, with NULLs in places where there is no match.

Syntax

				
					SELECT columns
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
				
			

5. CROSS JOIN

CROSS JOIN returns the Cartesian product of both tables, combining all rows from the first table with all rows from the second table.

Syntax

				
					SELECT columns
FROM table1
CROSS JOIN table2;
				
			

6. SELF JOIN

A SELF JOIN is a join in which a table is joined with itself. It is often used to compare rows within the same table.

Syntax

				
					SELECT a.columns, b.columns
FROM table a, table b
WHERE a.common_column = b.common_column;
				
			

Practical Use Cases for Joins

  1. Reporting: Generating comprehensive reports that combine data from multiple tables.
  2. Data Analysis: Analyzing relationships and correlations between different entities.
  3. Dashboards: Creating visualizations that require data from various sources.
  4. Data Migration: When consolidating or migrating data, joins help ensure relationships are maintained.
Previous
Next
SQL INNER JOIN
SQL INNER JOIN An INNER JOIN is a type of join that returns rows from multiple tables where there is...
NoSQL databases
What is NOSQL? NoSQL databases are a category of database management systems that do not adhere to the...
SQL BEGIN
SQL BEGIN  BEGIN: Starts a new transaction Syntax of BEGIN The syntax for starting a transaction can...
SQL-SELF JOIN
SQL SELF JOIN A self join is a type of join where a table is joined with itself. This can be particularly...
SQL JOINS
SQL JOIN operation A join is a SQL operation used to combine rows from two or more tables based on a...
Database Engines
Database Engines What is a Database Engine? A database engine is the underlying component of a database...
SQL-CEILING function ()
SQL CEILING function The CEILING function in SQL is a mathematical function used to round a numeric value...
SQL COMMIT statement
SQL COMMIT statement The COMMIT statement is used to save all changes made during the current transaction....
SQL-User Defined Function
What is a User-Defined Function? A User-Defined Function is a stored routine in SQL that can take parameters,...
SQL-Scalar Function
What is scalar function in SQL A scalar function is a user-defined or built-in function that takes one...