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.

Similar Reads

Types of join:

Inner join Full join (outer join) Left join Right join Cross join...

Inner Join

The inner join is one of the most common types of joins. Inner join is used to join two tables based on common characteristics among the rows. it returns a table that has common row characteristics....

Full Join

...

Left Join

It is also called ‘Full Outer Join’,.it returns all those data which either have a match in the left or right tables. if rows in both the tables do not match, the resulting data frame will replace NaN with every column of the tables that deficits a matching row....

Right Join

...

Cross 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....