Left Join
It is also known as Left Outer Join, returns a table containing all the rows of the left data frame. if there are non-matching rows of the left table then the unmatched data in the right table is replaced by NaN.
Execute a SQL statement:
SELECT table1.col1, table2.col2…
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
Code:
Python3
import psycopg2 conn = psycopg2.connect( database = "EMPLOYEE_DATABASE" , user = 'postgres' , password = 'pass' , host = '127.0.0.1' , port = '5432' ) conn.autocommit = True cursor = conn.cursor() sql = '''SELECT * from employee left JOIN dept\ ON employee.deptno =dept.deptno ''' cursor.execute(sql) results = cursor.fetchall() for i in results: print (i) conn.commit() conn.close() |
Output:
Python PostgreSQL – Join
In this article, we are going to see join methods in PostgreSQL using pyscopg2 in Python. Let’s see the type of joins supported in PostgreSQL.