Posted on: January 19, 2025 Posted by: rahulgite Comments: 0

1. Introduction to SQL Joins

SQL Joins are used to combine rows from two or more tables based on a related column. They are essential for querying data stored across multiple tables in a relational database.


2. Types of SQL Joins

2.1 INNER JOIN

Retrieves records that have matching values in both tables.

Syntax:

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Example: Consider two tables:

  • students:
idnamecourse_id
1John101
2Jane102
3AliceNULL
  • courses:
idname
101Math
102Science
103History

Query:

SELECT students.name, courses.name AS course
FROM students
INNER JOIN courses ON students.course_id = courses.id;

Result:

namecourse
JohnMath
JaneScience

2.2 LEFT JOIN (OUTER JOIN)

Retrieves all records from the left table and matching records from the right table. Unmatched rows in the right table result in NULL.

Syntax:

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

Example: Query:

SELECT students.name, courses.name AS course
FROM students
LEFT JOIN courses ON students.course_id = courses.id;

Result:

namecourse
JohnMath
JaneScience
AliceNULL

2.3 RIGHT JOIN (OUTER JOIN)

Retrieves all records from the right table and matching records from the left table. Unmatched rows in the left table result in NULL.

Syntax:

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

Example: Query:

SELECT students.name, courses.name AS course
FROM students
RIGHT JOIN courses ON students.course_id = courses.id;

Result:

namecourse
JohnMath
JaneScience
NULLHistory

2.4 FULL JOIN (FULL OUTER JOIN)

Retrieves all records from both tables, with NULLs where there is no match.

Syntax:

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

Example: Query:

SELECT students.name, courses.name AS course
FROM students
FULL JOIN courses ON students.course_id = courses.id;

Result:

namecourse
JohnMath
JaneScience
AliceNULL
NULLHistory

2.5 CROSS JOIN

Returns the Cartesian product of two tables. Every row in the first table is paired with every row in the second table.

Syntax:

SELECT columns
FROM table1
CROSS JOIN table2;

Example: Query:

SELECT students.name, courses.name AS course
FROM students
CROSS JOIN courses;

Result:

namecourse
JohnMath
JohnScience
JohnHistory
JaneMath
JaneScience
JaneHistory
AliceMath
AliceScience
AliceHistory

2.6 SELF JOIN

A table is joined with itself to compare rows within the same table.

Syntax:

SELECT a.column1, b.column2
FROM table a, table b
WHERE condition;

Example: Consider the employees table:

idnamemanager_id
1JohnNULL
2Jane1
3Alice1
4Bob2

Query:

SELECT e1.name AS employee, e2.name AS manager
FROM employees e1
LEFT JOIN employees e2 ON e1.manager_id = e2.id;

Result:

employeemanager
JohnNULL
JaneJohn
AliceJohn
BobJane

3. Summary of SQL Joins

Join TypeDescription
INNER JOINMatches records in both tables.
LEFT JOINAll records from the left, matched from right.
RIGHT JOINAll records from the right, matched from left.
FULL JOINAll records from both tables.
CROSS JOINCartesian product of both tables.
SELF JOINJoin within the same table.

Leave a Comment