Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
MySQL | Regular Expressions (Regexp)
Next article icon

JOINclause is used to combine rows from two or more tables based on a related column. It allows you to query data from multiple tables and return the result as a single set. There are several types of joins in SQL, each providing different results based on how data is combined.

JOIN
JOIN

Tables used in this article:

db4
student table of college database
db5
studentdetails table of college database

Types of Joins:

1. INNER JOIN

Returns records that have matching values in both tables. If no match is found, no record is returned.

Syntax:

SELECT column1, column2...
FROM table1
JOIN table2 ON table1.column = table2.column;

Example: Using INNER JOIN

In this example, we are using the INNER JOIN to retrieve the names of students who are present in both the Student table and the Student table (you can change the table names based on your requirements). This query returns the records where the Roll_no matches in both tables.

Python
importmysql.connector# Connecting to the databasemydb=mysql.connector.connect(host='localhost',database='College',user='root',# Replace with your MySQL usernamepassword='your_password'# Replace with your MySQL password)cs=mydb.cursor()# INNER JOIN: only students with matching detailsstatement="""SELECT s.Name, d.Branch               FROM Student s               INNER JOIN StudentDetails d ON s.Roll_no = d.Roll_no"""cs.execute(statement)# Fetch and print the resultsresult_set=cs.fetchall()forxinresult_set:print(x)# Disconnecting from the databasemydb.close()

Output:

db4_1
output in the terminal

2. LEFT JOIN

Returns all records from the left table and the matched records from the right table. If no match is found, the result is NULL on the right side.

Syntax:

SELECT column1, column2...
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

Example: Using LEFT JOIN

In this example, we use LEFT JOIN to fetch all student names from the STUDENT table and any matching records from the Student table. If a record doesn't have a match, the result will display NULL for columns from the right table.

Python
importmysql.connector# Connecting to the databasemydb=mysql.connector.connect(host='localhost',database='College',user='root',password='your_password')cs=mydb.cursor()# Left joinstatement="""SELECT s.Name, d.Section               FROM Student s               LEFT JOIN StudentDetails d ON s.Roll_no = d.Roll_no"""cs.execute(statement)forrowincs.fetchall():print(row)mydb.close()

Output:

db5_1
output of left join in terminal

3. RIGHT JOIN

Returns all records from the right table and the matched records from the left table. If no match is found, the result is NULL on the left side.

Syntax:

SELECT column1, column2...
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;

Example: Using RIGHT JOIN

In this example, we demonstrate the RIGHT JOIN, which returns all records from the Student table and the matching records from the STUDENT table. If no match is found, the result will show NULL for columns from the left table.

Python
importmysql.connectormydb=mysql.connector.connect(host='localhost',database='College',user='root',password='your_password')cs=mydb.cursor()#Right Joinstatement="""SELECT d.Branch, s.Name               FROM Student s               RIGHT JOIN StudentDetails d ON s.Roll_no = d.Roll_no"""cs.execute(statement)forrowincs.fetchall():print(row)mydb.close()

Output:

db6_1
output after performing right join

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp