Join allows you to combine two or more tables in SQL, based on the related column between them. It’s important to keep in mind that for this operation to work, there needs to be a common column in both tables. Based on this application of join there are three types of join:
SELECT column1, column2...
FROM tablename
JOIN tablename ON condition;
SELECT column1, column2...
FROM tablename
LEFT JOIN tablename ON condition;
SELECT column1, column2...
FROM tablename
RIGHT JOIN tablename ON condition;
import mysql.connector
# Connecting to the database
mydb = mysql.connector.connect(
host ='localhost',
database ='College',
user ='root',
)
cs = mydb.cursor()
# STUDENT and STudent are
# two different database
statement ="SELECT S.NAME from Student S JOIN \
Student on S.Roll_no = Student.Roll_no"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print(x)
import mysql.connector
# Connecting to the database
mydb = mysql.connector.connect(
host ='localhost',
database ='College',
user ='root',
)
cs = mydb.cursor()
# STUDENT and STudent are
# two different database
statement ="SELECT S.Name from STUDENT S\
LEFT JOIN Student s ON S.Roll_no = s.Roll_no"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print(x)
import mysql.connector
# Connecting to the database
mydb = mysql.connector.connect(
host ='localhost',
database ='College',
user ='root',
)
cs = mydb.cursor()
# STUDENT and STudent are
# two different database
statement ="SELECT S.Name from STUDENT S RIGHT \
JOIN Student s ON S.Roll_no = s.Roll_no"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print(x)
The above codes will output the result according to the used join type.
Note: also read about Limit Clause – MySQL
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…