The Wayback Machine - https://web.archive.org/web/20241004121826/https://www.geeksforgeeks.org/mysql-inner-join/
Open In App

MySQL Inner Join

Last Updated : 19 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In MySQL, the INNER JOIN clause is used to combine rows from two or more tables based on a related column between them. The INNER JOIN returns rows when there is at least one match in both tables.

If there are rows in the left table that do not have matches in the right table, those rows will not be included in the result set. This is one of the most commonly used types of joins.

In this article, we will look at what is “MySQL INNER JOIN” along with syntax and how it can be used with various keywords and clauses like GROUP BY, HAVING, WHERE, USING, and SQL Operators with the help of various examples.

MySQL INNER JOIN

The INNER JOIN keyword in MySQL selects only those tuples from both tables that satisfy the join condition. It creates the resultant set by joining all tuples from both tables where the value of the common column is the same.

Syntax:

SELECT <table1.column1>,<table1.column2>,….,<table2.column1>,…..

FROM table1 INNER JOIN table2

ON table1.condition_column = table2.condition_column

WHERE [condition];

GROUP BY <group-by-column-name>

HAVING [condition];

Parameters:

  • SELECT: Columns to retrieve from the resultant set.
  • INNER JOIN: Joins table1 with table2 based on a matching condition.
  • ON: Condition for how the tables are related (table1.condition_column = table2.condition_column).
  • WHERE: Optional filter condition for rows after the join operation.
  • GROUP BY: Optional clause to group the result set by specified columns.
  • HAVING: Optional condition to filter grouped rows based on aggregate functions.

Examples of MySQL INNER JOIN

Let us consider the following schemas for the examples in the following article.

StudentDetails Table:

id sname age university
1 Girish 24 IIT Hyderabad
2 Aaditya 24 SRM University
3 Aashish 23 IIT Hyderabad
4 John 25 Mumbai University
5 Shruti 24 IIT Hyderabad
6 Leena 25 Mumbai University

CourseDetails Table:

cid cname ratings price
1 Python Fundamentals 4.6 2999
2 Machine Learning 4.3 1999
3 DSA A-Z 4.9 5999
4 Competitive Programming 4.7 4999
5 Android Programming 4.6 4999

EnrolledIn Table:

sid cid
1 3
1 4
2 1
2 3
3 3
4 1

Example 1: MySQL INNER JOIN using Multiple Tables

We can also inner join to join more than two tables using the joining column condition.

Query:

Find the list of students of students enrolled in the “Python Fundamentals” course and output their details like student id, name,, and age.

SELECT S.sid, S.sname, S.age 
FROM StudentDetails S
INNER JOIN EnrolledIn E ON S.sid = E.sid
INNER JOIN CourseDetails C ON C.cid = E.cid
WHERE C.cname = "Python Fundamentals";

Output:

INNER-JOIN---Multiple-tables
INNER JOIN using Multiple Tables

Explanation: Our query needs the joining of StudentDetails, CourseDetails, and EnrolledIn Tables. The StudentDetails and EnrolledIn tables will be joined based on the sid column from both tables and then joined with the CourseDetails table based on the cid column. We also add a WHERE clause and check if the cname from the CourseDetails table is “Python Fundamentals“.

Example 2: MySQL INNER JOIN with Group By & HAVING Clause

You can use GROUP BY and HAVING clauses along with MySQL Inner Join to write complex queries.

Query:

Find the list of courses where more than one student is enrolled and output the course name and count of students enrolled in that course.

SELECT C.cname, count(*)
FROM StudentDetails S
INNER JOIN EnrolledIn E ON S.sid = E.sid
INNER JOIN CourseDetails C ON C.cid = E.cid
GROUP BY C.cname
HAVING count(*) > 1;

Output:

GROUP-BY
MySQL INNER JOIN with GROUP BY and HAVING clause

Explanation: The output lists courses (“Python Fundamentals” and “DSA A-Z“) with more than one student enrolled. It reflects the result of joining StudentDetails, EnrolledIn, and CourseDetails tables, grouping by course name (cname), and counting enrolled students (COUNT(*)).

Example 3: MySQL INNER JOIN with WHERE keyword

You can use WHERE keyword to further filter out the tuples that satisfy the particular condition specified in the WHERE clause.

Query:

Find the list of students from “IIT Hyderabad” university who have enrolled in at least one course and output their details like student id, name, and age.

SELECT distinct(S.sid), S.sname, S.age
FROM StudentDetails S
INNER JOIN EnrolledIn E ON S.sid = E.sid
WHERE S.university = "IIT Hyderabad";

Output:

INNER-JOIN---WHERE
INNER JOIN with WHERE clause

Explanation: Our query needs an INNER JOIN of two tables, StudentDetails & EnrolledIn, with the condition that the student should belong to “IIT Hyderabad” university. The StudentDetails and EnrolledIn table joined on the sid column and its result with the CourseDetails table on the cid column with a WHERE condition for specifying the University name. Of the three students at “IIT Hyderabad” university, only “Girish” and “Aashish” have enrolled in at least one course.

Example 4: MySQL INNER JOIN with USING clause

If the name of the column on which two tables are to be joined is the same in both tables, then we can use the USING clause, which takes the joining column name as input.

Query:

Find the list of students from “IIT Hyderabad” university who have enrolled in at least one course and output their details like student id, name, and age.

SELECT distinct(S.sid), S.sname, S.age
FROM StudentDetails S
INNER JOIN EnrolledIn E USING(sid)
WHERE S.university = "IIT Hyderabad";

Output:

INNER-JOIN---USING-clause
INNER JOIN – USING clause

Explanation: Again an INNER JOIN of two tables, StudentDetails & EnrolledIn, with a condition that the student should belong to “IIT Hyderabad” university. This query asks to output the exact same thing asked in the example in point 4. Instead of using the ON keyword and mentioning the joining condition, we used the USING clause with the column name. Since the column name of the joining column, i.e. sid is the same, we will use the USING(sid) clause.

Example 5: MySQL INNER JOIN using Operators

We can use various different SQL Operators along with MySQL INNER JOIN, these operators can be arithmetic operators such as +, -, *, /, %, or they can be comparison operations such as equal (=), not equal (!=), greater than (>), less than (<) or they can even be logical operators like AND, OR, ANY, BETWEEN, EXISTS, etc.

Query:

Find the list of courses where at least one student is enrolled and the price of the course is greater than 4000 and output their course name, price, and ratings.

SELECT distinct(C.cname), C.price, C.ratings
FROM CourseDetails C
INNER JOIN EnrolledIn E ON C.cid = E.cid
WHERE C.price > 4000;

Output:

INNER-JOIN---Operators
MySQL INNER JOIN using operators

Explanation: Our query will consist of CourseDetails and EnrolledIn tables and will be joined based on the cid column from both tables. We add a WHERE clause and check if the price > 4000 to output courses with a price > 4000 and at least one student is enrolled. We have used the greater than (>) comparison operator in this query.

Conclusion

So, INNER JOIN in MySQL joins two or more tables and matches the rows based on the joining column condition. Only those rows appear in the resultant set where the condition is satisfied. In this article, we first looked at what MySQL INNER JOIN is and its syntax. Then, using various examples, we saw how MySQL INNER JOIN can be used for Multiple Tables, with GROUP BY and HAVING clause, WHERE keyword, USING clause and along with various operators such as arithmetic, comparison, logical, bitwise, etc.

FAQs on MySQL INNER JOIN

What is an INNER JOIN in MySQL?

An INNER JOIN in MySQL is used to combine rows from two or more tables based on a related column between them. It retrieves rows from both tables that have matching values in the specified column or columns.

When should I use INNER JOIN in MySQL?

You should use INNER JOIN in MySQL when you want to retrieve rows from multiple tables that have matching values in a common column. It’s useful for querying related data stored across different tables in a database.

Can INNER JOIN be used with multiple tables in MySQL?

Yes, INNER JOIN can be used with multiple tables in MySQL. You can join more than two tables by chaining INNER JOIN clauses together, specifying the join conditions for each pair of tables.



Previous Article
Next Article

Similar Reads

Full join and Inner join in MS SQL Server
Full Join Full join selects all the rows from the left and right tables along with the matching rows as well. If there are no matching rows, it will be displayed as NULL. Syntax: select select_list from table1 full join table2 on join _predicate (OR) select table1.*, table2.* from table1 full join table2 on join _predicate (OR) select * from table1
2 min read
Difference between Inner Join and Outer Join in SQL
JOINS in SQL are fundamental operations used to combine data from multiple tables based on related columns. They are essential for querying data that is distributed across different tables, allowing you to retrieve and present it as a single or similar result set. In this article, We will learn about Inner Join vs Outer Join in SQL in detail. What
5 min read
Difference Between “INNER JOIN” and “OUTER JOIN”
Joins in SQL are essential tools that allow us to combine rows from multiple tables based on specific conditions, often involving a relation between columns in those tables. These joins help in pulling related data together in a meaningful way. Among the most commonly used joins are INNER JOIN and OUTER JOIN. In this article, We will learn about th
5 min read
When should we use CROSS APPLY over INNER JOIN in MySQL
CROSS APPLY and INNER JOIN are used in MySQL to combine data from multiple tables. When dealing with row-wise operations or dynamic subqueries, CROSS APPLY is more efficient than INNER JOIN. CROSS APPLY allows for more detailed control on individual rows, while INNER JOIN operates on sets of data. It becomes very useful when we need to perform row-
4 min read
INNER JOIN ON vs WHERE clause in MySQL
When working with MySQL queries that involve multiple tables, understanding how to effectively use INNER JOIN ON versus the WHERE clause can significantly impact query performance and clarity. These two SQL constructs serve distinct purposes in combining data from different tables based on specific conditions. In this article, we will explore the d
5 min read
CROSS APPLY vs INNER JOIN in PL/SQL
PL/SQL Stands for procedural language extension to SQL. In a procedure, the role of the subprogram is to perform a particular task and it is a unit module of a program. It combined to form larger programs. A subprogram can be involved by another program which is called the calling program. PL/SQL provides a block structure of executable unit code.
6 min read
How Inner Join works in LINQ to SQL
LINQ (Language Integrated Query) in C# provides a powerful way to query data from various data sources, including databases. In LINQ to SQL, which is specifically designed for working with relational databases, an inner join is a common operation used to combine records from two tables based on a specified condition. This article explores the conce
3 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL Join operation combines data or rows from two or more tables based on a common field between them. In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples. SQL JOINSQL JOIN clause is used to query and access data from multiple tables by establishing logical relationships between them. It can access data from
5 min read
INNER JOIN ON vs WHERE clause in SQL Server
In SQL Server, joining tables and filtering data are essential for retrieving meaningful information. The INNER JOIN operation is used to combine rows from multiple tables based on a matching condition, while the WHERE clause allows for further filtering of results. In this article, we will PL/SQL Show Indexes by understanding its syntax and variou
7 min read
When Should We Use CROSS APPLY Over INNER JOIN?
In SQL Server, both INNER JOIN and CROSS APPLY are used to combine data from multiple tables, but they serve different purposes and have distinct use cases. INNER JOIN is typically used to match rows between two tables based on a related column, returning only the rows where a match exists in both tables. On the other hand, CROSS APPLY allows for m
4 min read
How to Delete using INNER JOIN with SQL Server?
In SQL Server, we can use INNER JOIN within a DELETE statement to remove data from one table based on matching records in another table. This method is useful when we need to delete records from a target table that have corresponding rows in another table. Understanding DELETE with INNER JOINUnderstanding DELETE with INNER JOIN in SQL Server enable
4 min read
Difference Between Anti-Join and Semi-Join
In the context of SQL, Anti-join, and semi-join are two essential operations in relational databases used for querying and manipulating data. These operations focus on comparing data from two related tables, but they serve distinct purposes. In this article let us discuss these two operations in detail along with some examples. What is SQL Join?Joi
5 min read
Difference Between Left Join and Left Outer Join
In SQL language, different joins are used to assemble rows from two or more tables from the related column. The terms "Left Join" and "Left Outer Join" are used interchangeably in SQL but they refer to the same concept. A Left Join retrieves all records from the left table (the first table in the query) and matches them with records from the right
5 min read
Difference Between Right Join and Right Outer Join
Joins in a Database (SQL) are mostly used for combining data or the rows of two or more table records that are based on the same or common attribute. There are various types of Joins like Right Join, Left Join, Full Join, etc. Each join has its own syntax and data-returning capability. In this article, we will see the information about Right Join a
5 min read
MySQL UPDATE JOIN
A widely used open-source relational database management system that allows you to efficiently store, organize, and retrieve data. Developed by Oracle, My SQL is widely used for building and managing databases that handle interactive websites and applications. We'll discuss the syntax, and demonstrate how this dynamic duo can efficiently modify dat
6 min read
How to Use Full Outer Join in MySQL
The FULL OUTER JOIN or FULL JOIN in MySQL is a powerful technique to fetch all rows from both tables, including matched and unmatched records. In this article, we will explore how to use FULL OUTER JOIN with practical examples and MySQL queries. Although MySQL doesn't natively support FULL OUTER JOIN, we can achieve the same result using a combinat
4 min read
MySQL SELF JOIN
Joins are very important for effective data retrieval and analysis. The 'JOIN' clause is used to combine data from two or more tables using the common column between them. In MySql, there are many types of joins like INNER JOIN, OUTER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and SELF JOIN. In this article, we'll explore the concept of MySQL SELF JOI
5 min read
MySQL LEFT JOIN
In databases, data is often stored in multiple tables, making it necessary to combine them to fetch required information. MySQL JOIN statements enable merging tables based on common columns. In this article, we'll explore the MySQL LEFT JOIN keyword, a type of outer join that returns all records from the left table and matched records from the righ
5 min read
MySQL DELETE JOIN
MySQL is an open-source, user-friendly, powerful, and popular choice, relational database management system. When maintaining and modifying data, tables usually interact in a complex way. MySQL's DELETE JOIN function is one of its most powerful functions. MySQL DELETE JOIN is explored in detail in this article, which also offers examples to help vi
4 min read
MySQL RIGHT JOIN
In databases, data is stored in multiple tables and it is often necessary sometimes to combine two or more tables to fetch the required data. In MySQL, joins enable the merging of multiple tables based on the common columns. In this article, we are going to explore MySQL RIGHT JOINS which is a type of outer join in MySQL. MySQL RIGHT JOINIn MySQL,
5 min read
MySQL CROSS JOIN
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. Among these operations, MySQL also provides the CROSS JOIN statement to combine
5 min read
MySQL Outer Join
MySQL is a popular and strong system that is extensively employed for handling data in the field of relational databases. A crucial aspect of working with databases is the ability to retrieve and handle data from different tables. SQL joins combine rows from different tables by connecting them based on related columns. The Outer Join is an importan
4 min read
MySQL JOIN
MySQL JOIN is a fundamental feature that combines rows from two or more tables based on a related column between them. It allows for efficient data retrieval by enabling the extraction of related information from multiple tables in a single query. In this article, We will learn about MySQL JOIN by understanding various types of JOINS with the help
6 min read
Functions in MySQL | Numeric, String and Date Time Functions in MySQL
In MySQL, functions play a crucial role in performing various operations on data, such as calculations, string manipulations, and date handling. These built-in functions simplify complex queries and data transformations, making it easier to manage and analyze data within a database. In this article, we will look at these different categories of MyS
4 min read
SQL Server FULL OUTER JOIN
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN. Instead of discarding a row with no match, it fi
6 min read
SQL Server UPDATE JOIN
In the expansive realm of SQL Server, the UPDATE JOIN operation emerges as a potent tool for modifying data within tables by combining information from multiple sources. Unlike a traditional UPDATE statement that modifies a single table, UPDATE JOIN enables the modification of records based on conditions involving multiple tables. This capability p
6 min read
How to Join First Row in SQL?
SQL(Structured Query Language) is a powerful tool that is used to manage and manipulate data in a database. Join is a very important tool in SQL that combines multiple tables based on related columns between them. Joining the first row of tables in SQL is a very useful technique when you need to fetch additional information from the tables. In this
5 min read
FULL OUTER JOIN in SQLite
In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge data from two tables, even if there are no direct m
4 min read
How to Join to First Row in SQL Server
Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, We will learn about How to Join to first row in SQL
4 min read
How to Perform the SQL Join Equivalent in MongoDB?
In database management, with the rise of NoSQL databases like MongoDB, the approach to handling data relationships has evolved. MongoDB's document-oriented nature and schema-less design provide a different perspective on joins compared to SQL. In this article, we'll learn about How to Perform the SQL Join Equivalent in MongoDB by understanding thei
6 min read