Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Join Operations in SQL with Python - Join Operations in SQL with Python

Join Operations in SQL with Python

Posted on December 25, 2024December 25, 2024 By Rishabh saini No Comments on Join Operations in SQL with Python

Join Operations in SQL

Data often resides in multiple tables in databases, and combining this data effectively is crucial for meaningful insights. SQL join operations are useful in this situation. Rows from two or more tables can be combined by using a common column. In this blog, we’ll explore join operations using an example in Python with MySQL, explained in a professional yet approachable tone.

Download New Real Time Projects :-Click here

Join Operations in SQL with Python
Join Operations in SQL with Python

Creating Tables

Let’s begin by creating a new table, Departments, alongside an existing Employee table.

CREATE TABLE Departments (
    Dept_id INT(20) PRIMARY KEY NOT NULL,
    Dept_Name VARCHAR(20) NOT NULL
);

Now, insert some values into the Departments table:

INSERT INTO Departments VALUES (201, 'CS');
INSERT INTO Departments VALUES (202, 'IT');

Similarly, ensure the Employee table contains relevant records with a column Dept_id to match the department details.

Join Operations in Action

Inner Join

The Inner Join retrieves records matching a condition in both tables. Here, we’ll fetch employee details along with their department names using the common column Dept_id.

Python Code for Inner Join

import mysql.connector

# Create the connection object
myconn = mysql.connector.connect(
    host="localhost", user="root", passwd="yourpassword", database="PythonDB"
)

# Create the cursor object
cur = myconn.cursor()

try:
    # Join query
    cur.execute("""
        SELECT Employee.id, Employee.name, Employee.salary, 
               Departments.Dept_id, Departments.Dept_Name 
        FROM Departments 
        JOIN Employee ON Departments.Dept_id = Employee.Dept_id
    """)
    print("ID    Name    Salary    Dept_Id    Dept_Name")
    for row in cur:
        print(f"{row[0]}    {row[1]}    {row[2]}    {row[3]}    {row[4]}")

except Exception as e:
    print("Error:", e)
    myconn.rollback()

myconn.close()

Output:

IDNameSalaryDept_IdDept_Name
101John25000201CS
103David25000202IT

Right Join

The Right Join includes all records from the right table (Employee) and matching records from the left table (Departments). It helps identify employees without a department.

To demonstrate, let’s add a record for an employee who doesn’t belong to any department:

INSERT INTO Employee (id, name, salary, branch_name) 
VALUES (108, 'Alex', 29900, 'Mumbai');

Python Code for Right Join

try:
    cur.execute("""
        SELECT Employee.id, Employee.name, Employee.salary, 
               Departments.Dept_id, Departments.Dept_Name 
        FROM Departments 
        RIGHT JOIN Employee ON Departments.Dept_id = Employee.Dept_id
    """)
    print("ID    Name    Salary    Dept_Id    Dept_Name")
    for row in cur:
        print(f"{row[0]}    {row[1]}    {row[2]}    {row[3]}    {row[4]}")

except Exception as e:
    print("Error:", e)
    myconn.rollback()

myconn.close()

Output:

IDNameSalaryDept_IdDept_Name
101John25000201CS
108Alex29900NULLNULL

Left Join

The Left Join includes all records from the left table (Departments) and matching records from the right table (Employee). It ensures no department data is missed.

Python Code for Left Join

try:
    cur.execute("""
        SELECT Employee.id, Employee.name, Employee.salary, 
               Departments.Dept_id, Departments.Dept_Name 
        FROM Departments 
        LEFT JOIN Employee ON Departments.Dept_id = Employee.Dept_id
    """)
    print("ID    Name    Salary    Dept_Id    Dept_Name")
    for row in cur:
        print(f"{row[0]}    {row[1]}    {row[2]}    {row[3]}    {row[4]}")

except Exception as e:
    print("Error:", e)
    myconn.rollback()

myconn.close()

Output:

Dept_IdDept_NameIDNameSalary
201CS101John25000
202IT103David25000

PHP PROJECT:- CLICK HERE


joins in sql with examples
self join in sql
types of joins in sql
cross join in sql
sql join 3 tables
inner join in sql
natural join in sql
left join in sql
Join Operations in SQL 
set operations in sql
aggregate functions in sql
join operations in sql w3schools
join operations in sql server
join operations in sql oracle
join operations in sql server with example
Join Operations in SQL with Python

Post Views: 685
Python Tags:how to use joins in sql, joins, joins in dbms, joins in sql, joins in sql server, joins in sql with examples, practice sql queries using joins, sql joins, sql joins diagram, sql joins explained, sql joins multiple tables, sql joins tutorial, sql joins tutorial for beginners, sql joins with example, sql left joins, sql multiple joins, sql outer joins, sql queries using joins, sql self joins, sql server joins, sql types of joins

Post navigation

Previous Post: Age Calculator in Python with Source Code
Next Post: AES-256 Encryption Using Python

More Related Articles

Password Manager App Using Python Password Manager App Using Python Python
Database Connection To Python Applications - Database Connection To Python Applications Database Connection To Python Applications Python
Age Calculator in Python with Source Code - Age Calculator in Python Age Calculator in Python with Source Code Python

Leave a Reply Cancel reply

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

You may also like

  1. Python Decorators: A Comprehensive Guide
  2. How to Calculate Distance between Two Points using GEOPY
  3. Rock, Paper, Scissors Game
  4. Fetcher App Using Python and Tkinter
  5. How to Install Django: Step-by-Step Guide
  6. Python Tkinter Canvas: A Guide to Structured Graphics in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,617)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,227)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,877)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme