
- Python Data Access Tutorial
- Python Data Access - Home
- Python MySQL
- Python MySQL - Introduction
- Python MySQL - Database Connection
- Python MySQL - Create Database
- Python MySQL - Create Table
- Python MySQL - Insert Data
- Python MySQL - Select Data
- Python MySQL - Where Clause
- Python MySQL - Order By
- Python MySQL - Update Table
- Python MySQL - Delete Data
- Python MySQL - Drop Table
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - Cursor Object
- Python PostgreSQL
- Python PostgreSQL - Introduction
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Create Database
- Python PostgreSQL - Create Table
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Select Data
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Order By
- Python PostgreSQL - Update Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - Cursor Object
- Python SQLite
- Python SQLite - Introduction
- Python SQLite - Establishing Connection
- Python SQLite - Create Table
- Python SQLite - Insert Data
- Python SQLite - Select Data
- Python SQLite - Where Clause
- Python SQLite - Order By
- Python SQLite - Update Table
- Python SQLite - Delete Data
- Python SQLite - Drop Table
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - Cursor Object
- Python MongoDB
- Python MongoDB - Introduction
- Python MongoDB - Create Database
- Python MongoDB - Create Collection
- Python MongoDB - Insert Document
- Python MongoDB - Find
- Python MongoDB - Query
- Python MongoDB - Sort
- Python MongoDB - Delete Document
- Python MongoDB - Drop Collection
- Python MongoDB - Update
- Python MongoDB - Limit
- Python Data Access Resources
- Python Data Access - Quick Guide
- Python Data Access - Useful Resources
- Python Data Access - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python PostgreSQL - Join
When you have divided the data in two tables you can fetch combined records from these two tables using Joins.
Example
Assume we have created a table with name CRICKETERS and inserted 5 records into it as shown below −
postgres=# CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); postgres=# insert into CRICKETERS values ( 'Shikhar', 'Dhawan', 33, 'Delhi', 'India' ); postgres=# insert into CRICKETERS values ( 'Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica' ); postgres=# insert into CRICKETERS values ( 'Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka' ); postgres=# insert into CRICKETERS values ( 'Virat', 'Kohli', 30, 'Delhi', 'India' ); postgres=# insert into CRICKETERS values ( 'Rohit', 'Sharma', 32, 'Nagpur', 'India' );
And, if we have created another table with name OdiStats and inserted 5 records into it as −
postgres=# CREATE TABLE ODIStats ( First_Name VARCHAR(255), Matches INT, Runs INT, AVG FLOAT, Centuries INT, HalfCenturies INT ); postgres=# insert into OdiStats values ('Shikhar', 133, 5518, 44.5, 17, 27); postgres=# insert into OdiStats values ('Jonathan', 68, 2819, 51.25, 4, 22); postgres=# insert into OdiStats values ('Kumara', 404, 14234, 41.99, 25, 93); postgres=# insert into OdiStats values ('Virat', 239, 11520, 60.31, 43, 54); postgres=# insert into OdiStats values ('Rohit', 218, 8686, 48.53, 24, 42);
Following statement retrieves data combining the values in these two tables −
postgres=# SELECT Cricketers.First_Name, Cricketers.Last_Name, Cricketers.Country, OdiStats.matches, OdiStats.runs, OdiStats.centuries, OdiStats.halfcenturies from Cricketers INNER JOIN OdiStats ON Cricketers.First_Name = OdiStats.First_Name; first_name | last_name | country | matches | runs | centuries | halfcenturies ------------+------------+-------------+---------+-------+-----------+--------------- Shikhar | Dhawan | India | 133 | 5518 | 17 | 27 Jonathan | Trott | SouthAfrica | 68 | 2819 | 4 | 22 Kumara | Sangakkara | Srilanka | 404 | 14234 | 25 | 93 Virat | Kohli | India | 239 | 11520 | 43 | 54 Rohit | Sharma | India | 218 | 8686 | 24 | 42 (5 rows) postgres=#
Joins using python
When you have divided the data in two tables you can fetch combined records from these two tables using Joins.
Example
Following python program demonstrates the usage of the JOIN clause −
import psycopg2 #establishing the connection conn = psycopg2.connect( database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432' ) #Setting auto commit false conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving single row sql = '''SELECT * from EMP INNER JOIN CONTACT ON EMP.CONTACT = CONTACT.ID''' #Executing the query cursor.execute(sql) #Fetching 1st row from the table result = cursor.fetchall(); print(result) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
Output
[('Ramya', 'Rama priya', 27, 'F', 9000.0, 101, 101, 'Krishna@mymail.com', 'Hyderabad'), ('Vinay', 'Battacharya', 20, 'M', 6000.0, 102, 102, 'Raja@mymail.com', 'Vishakhapatnam'), ('Sharukh', 'Sheik', 25, 'M', 8300.0, 103, 103, 'Krishna@mymail.com ', 'Pune'), ('Sarmista', 'Sharma', 26, 'F', 10000.0, 104, 104, 'Raja@mymail.com', 'Mumbai')]
Advertisements