MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the tables that match the specified condition and hides other rows and columns. MySQL assumes it as a default Join, so it is optional to use the Inner Join keyword with the query.

Syntax

The syntax of the inner join shows below. This syntax works on two tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
column data type constraint,
FROM table name1
INNER JOIN table2 ON condition1;

The syntax of the inner join shows below. This syntax works on multiple tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
column data type constraint,
FROM table name1
INNER JOIN table2 ON condition1
INNER JOIN table3 ON condition2;

Examples of the MySQL inner join

Let us understand it with the help of an example. First, we will create two tables in the MySQL system with constraints and a common column.

1) Example: the primary inner join example shows below.

Execute the below query to join two tables. The below statement will return all columns using the "INNER JOIN" query.

mysql> select d.department_name, d.admissions, s.subject_name, s.students
    -> FROM department d
    -> INNER JOIN subject s;

OUTPUT

INNER JOIN

The above image shows all the columns of the two tables. The "department_name", & "admissions" of the department table and "subject_name", & "students" columns of the subject table.

2) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables. We must specify the condition to join two tables.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

INNER JOIN

The above output image shows the combined result of the two tables. The "department_name" and "department_id" columns have in the department table. The "subject_name" and "students" columns have in the subject table.

3) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables.

mysql> select *
    -> FROM department d
    -> INNER JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

INNER JOIN

The above output image shows the combined columns of the two tables. For example, this query shows complete columns of the department and student tables.

4) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables. For example, this "INNER JOIN" query uses "WHERE" with "AND" conditions.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> AND  s.subject_name = "Electronics";

OUTPUT

INNER JOIN

This query shows complete columns of the department and student tables. The above image shows the combined columns of the two tables. The inner query works with the logical condition. Both conditions fulfill using "AND" logical condition.

5) Example: the inner join with multiple tables' example shows below.

Execute the below query to join two tables. Two tables join required common columns using conditions. For example, the "INNER JOIN" query uses "WHERE" with "OR" conditions.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Electronics";

OUTPUT

INNER JOIN

The above image shows the combined columns of the two tables. This query shows complete columns of the department and student tables. The inner query works with the logical condition. The "electronics" column and "bachelor of technology" column display both data rows of the table.

6) Example: the inner join with the "USING" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "USING" clause with the "subject_id" foreign key.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> USING (subject_id)

OUTPUT

INNER JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column links two tables using a foreign key. The above table is the result of the inner join query with the "USING" clause.

7) Example: the inner join with the "GROUP BY" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "GROUP BY" clause with the "subject_id" foreign key. It also uses AND logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> AND s.subject_name = "Computer Science"
    -> GROUP BY subject_id;

OUTPUT

INNER JOIN

The above output image displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key. The above table works inner join query with the "GROUP BY" clause.

8) Example: the inner join with the "ORDER BY" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "ORDER BY" and "using" clauses with the "subject_id" foreign key. The inner join uses OR logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Computer Science"
    -> ORDER BY subject_id DESC;

OUTPUT

INNER JOIN

The above output table displays combined columns of the department and subject tables. It is the result of the inner join query with the "USING" clause. Here, the table applies the inner join query with the "ORDER BY" clause. The table data display in descending order.