SQL JOIN IN PYTHON

In SQL, the JOIN clause is used to combine rows from two or more tables based on a related column between them.

Types of JOINs
1. INNER JOIN
An **INNER JOIN returns only the rows that have matching values in both tables.
SYNTAX

SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Enter fullscreen mode
Exit fullscreen mode
 _EXAMPLE_
Enter fullscreen mode
Exit fullscreen mode
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id 
Enter fullscreen mode
Exit fullscreen mode

EXPLANATIONS
**This query selects all employees and their corresponding department names where there is a match between employees.department_id and departments.id.

2. LEFT JOIN (or LEFT OUTER JOIN)
**A LEFT JOIN returns all rows from the left table (table1), and the matched rows from the right table (table2). If no match is found, NULL values are returned for columns from the right table.

_SYNTAX_
Enter fullscreen mode
Exit fullscreen mode
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column; 
Enter fullscreen mode
Exit fullscreen mode
_EXAMPLE_
Enter fullscreen mode
Exit fullscreen mode
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id; 
Enter fullscreen mode
Exit fullscreen mode

EXPLANATIONS

**This query selects all employees, including those who do not belong to any department. For employees without a department, department_name will be NULL.

3.RIGHT JOIN (or RIGHT OUTER JOIN)

**A RIGHT JOIN returns all rows from the right table (table2), and the matched rows from the left table (table1). If no match is found , NULL values are returned for columns from the left table.

_SYNTAX_
Enter fullscreen mode
Exit fullscreen mode
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column; 
Enter fullscreen mode
Exit fullscreen mode

EXAMPLE

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id; 
Enter fullscreen mode
Exit fullscreen mode
_EXPLANATIONS_
Enter fullscreen mode
Exit fullscreen mode

**This query selects all departments, including those without any employees. For departments without employees, name will be NULL.

4.FULL JOIN (or FULL OUTER JOIN)

**A FULL JOIN returns all rows when there is a match in either left (table1) or right (table2) table. Rows without a match in one of the tables will have NULLs for columns from that table.

SYNTAX

SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column; 
Enter fullscreen mode
Exit fullscreen mode

EXAMPLE

SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.id; 
Enter fullscreen mode
Exit fullscreen mode
_EXPLANATIONS_
Enter fullscreen mode
Exit fullscreen mode
This query selects all employees and departments, showing NULLs where there is no match.
Enter fullscreen mode
Exit fullscreen mode

5.CROSS JOIN
**A CROSS JOIN returns the Cartesian product of the two tables, i.e., it returns all possible combinations of rows from the tables.

_SYNTAX_
Enter fullscreen mode
Exit fullscreen mode
SELECT columns
FROM table1
CROSS JOIN table2; 
Enter fullscreen mode
Exit fullscreen mode
_EXAMPLE_
Enter fullscreen mode
Exit fullscreen mode
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments; 
Enter fullscreen mode
Exit fullscreen mode

EXPLANATIONS
**This query combines each employee with each department, resulting in a large number of rows.

6.SELF JOIN
**A SELF JOIN is a regular join, but the table is joined with itself.

SYNTAX

SELECT a.columns, b.columns
FROM table a, table b
WHERE a.common_column = b.common_column; 
Enter fullscreen mode
Exit fullscreen mode

EXAMPLE

SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id = e2.id; 
Enter fullscreen mode
Exit fullscreen mode

Key Points:
i. JOIN Conditions:The ON clause specifies the condition for the join, typically matching a column from one table with a column from another table.
ii. Aliases:Using table aliases (e.g., e1, e2 in the SELF JOIN example) can make the query more readable and manageable, especially for self joins or when the same table is used multiple times.
iii. NULL Handling:In OUTER JOINs (LEFT, RIGHT, FULL), unmatched rows will have NULL values for the columns of the table that does not have the match.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
This is why our affordable cloud storage services are structured to fit a wide range of financial needs.