Python SQLite – JOIN Clause
Last Updated :
23 May, 2021
In this article, we discuss the JOIN clause in SQLite using the sqlite3 module in Python. But at first let’s see a brief about join in SQLite.
Join Clause
A JOIN clause combines the records from two tables on the basis of common attributes. The different types of joins are as follows:
- INNER JOIN (OR JOIN) – Gives the records that have common attributes in both tables.
- LEFT JOIN – Gives all records from the left table and only the common records from the right table.
- RIGHT JOIN – Gives all records from the right table and only the common records from the left table.
- FULL OUTER JOIN – Gives all records when there is a common attribute in either the left or the right table.
- CROSS JOIN – Gives records of one table with all other records of another table.
Note:
- Unlike other types of joins, it does not include a join condition.
- SQLite does not directly support the RIGHT JOIN and FULL OUTER JOIN.
Creating a Database
Here, we will create a simple database having two tables Advisor(AdvisorID, AdvisorName) and Student(StudentID, StudentName, AdvisorID) where AdvisorID of the Student table is the foreign key referencing AdvisorID of the Advisor table.
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
cursor.executescript(
)
conn.commit()
conn.close()
|
Tables Created:
Advisor Table
Student Table
Now, let’s perform different types of join on the above-created database.
INNER JOIN
Inner join also represented as join which gives the records that have common attributes in both tables.
Syntax:
SELECT columns
FROM table1
[INNER] JOIN table2
ON table1.column = table2.column;
INNER keyword is optional
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
sql =
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print (row)
conn.close()
|
Output:
LEFT JOIN
Gives all records from the left table, and only the common records from the right table.
Syntax:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
OUTER keyword is optional
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
sql =
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print (row)
conn.close()
|
Since the column name (AdvisorID) of joined tables is same, the clause USING(AdvisorID) can be used instead of ON Student.AdvisorID = Advisor.AdvisorID.
Output:
RIGHT JOIN
Gives all records from the right table, and only the common records from the left table. As mentioned before, SQLite does not directly support RIGHT JOIN. However, it can be emulated using LEFT JOIN by switching the positions of the student and advisor table.
Syntax:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
OUTER keyword is optional
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
sql =
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print (row)
conn.close()
|
Output:
FULL OUTER JOIN
Gives all records when there is a common attribute in either left or the right table. As mentioned before, SQLite does not directly support FULL OUTER JOIN. However, it can be emulated using LEFT JOIN. In this query, the second SELECT statement has the positions of the student and advisor table switched. The UNION ALL clause retains the duplicate rows from the result of both SELECT queries. And the WHERE clause in the second SELECT statement removes rows that already included in the result set of the first SELECT statement.
Syntax:
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;
OUTER keyword is optional
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
sql =
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print (row)
conn.close()
|
Output:
CROSS JOIN
It combines all records of one table with all other records of another table, that is, it creates a Cartesian product of records from the join tables.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Python3
import sqlite3
conn = sqlite3.connect(r 'C:\Users\SQLite\Geeks.db' )
cursor = conn.cursor()
sql =
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print (row)
conn.close()
|
Output:
Share your thoughts in the comments
Please Login to comment...