SQL INNER JOIN Keyword
INNER JOIN is one of the most commonly used join types in SQL, used to extract matching records from multiple tables based on the relationships between them.
The INNER JOIN keyword returns rows when there is at least one match in the tables; it returns the intersection of the two tables that satisfies the join condition, that is, data present in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Parameter Description:
- columns: The names of the columns to display.
- table1: The name of table 1.
- table2: The name of table 2.
- column_name: The name of the column used for joining in the table.
**Note:** INNER JOIN is the same as JOIN.

Assume there are two tables: Students and Enrollments.
Students Table:
| StudentID | Name | Age |
|---|---|---|
| 1 | Alice | 22 |
| 2 | Bob | 23 |
| 3 | Charlie | 24 |
Enrollments Table:
| EnrollmentID | StudentID | Course |
|---|---|---|
| 101 | 1 | Math |
| 102 | 2 | Science |
| 103 | 4 | History |
Query Using INNER JOIN
SELECT Students.Name, Enrollments.Course
FROM Students
INNER JOIN Enrollments
ON Students.StudentID = Enrollments.StudentID;
The result is as follows:
| Name | Course |
|---|---|
| Alice | Math |
| Bob | Science |
Explanation:INNER JOIN returns a Students and Enrollments table StudentID matching data. Records without a match (such as Charlie and the record with EnrollmentID 103) will be excluded.
Demonstration Database
In this tutorial, we will use the RUNOOB sample database.
Below is the data selected from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
Below is the data from the "access_log" website access log table:
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
SQL INNER JOIN Example
The following SQL statement returns all website access records:
Example
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count;
The result of executing the above SQL is as follows:
**Note:** The INNER JOIN keyword returns rows when there is at least one match in the tables. If a row in "Websites" has no match in "access_log", that row is not listed.