×

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self join" connects with itself. The self join returns the record of the single table. It uses for structured data or hierarchical table format. For example, if you want to compare rows and columns in the table, use self join.

Syntax

The "self join" syntax shows below.

SELECT Column1 data type constraint,
Column2 data type constraint,
FROM table AS id1 
INNER JOIN table AS id2 condition; 

The "self join" with the "WHERE" clause syntax shows below.

SELECT Column1 data type constraint,
Column2 data type constraint,
FROM table id1, table id2 
WHERE id1.Column1 = id2.Column1; 

The "self join" with the "inner join" syntax shows below.

SELECT column list FROM table.id1 INNER JOIN table.id2 condition; 

The "self join" with the "self join" syntax shows below.

SELECT column list FROM table.id1 SELF JOIN table.id2 condition; 

Examples of the MySQL self join.

1) Example: the self joins with a single table example shows below.

Execute the below query to join two tables using the "SELF JOIN" query. This query uses the ON and WHERE clauses with SELF JOIN.

mysql> select
concat (d.department_id, ', ',  d.department_name) AS department,
concat (s. department_id, ', ', s. department_name) AS subject
FROM department d
INNER JOIN department s;

OUTPUT

SELF JOIN

The above output image shows the required columns of one table. The department and subject are two ids of the single table. The "department_name" and "department_id" columns show in the department table with a different id.

2) Example: the self join with inner join example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions.

mysql> select
concat (d.department_id, ', ',  d.department_name) AS department,
concat (s. department_id, ', ', s. department_name) AS subject
FROM department d
INNER JOIN department s
ORDER BY department;

OUTPUT

SELF JOIN

The above output image shows the required columns from a single table. The "department_name" and "department_id" columns show in the department table with a different id.

3) Example: the self join with the "on" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions.

mysql> select
    -> concat (d.department_id, ', ',  d.department_name) AS department,
    -> concat (s. department_id, ', ', s. department_name) AS subject
    -> FROM department d
    -> INNER JOIN department s
   -> ON d.department_id = s. department_id
    -> ORDER BY department DESC;

OUTPUT

SELF JOIN

The above output image shows the required columns of one table. The department and subject are two ids of the single table. The "department_name" and "department_id" columns show in the department table with a different id.

4) Example: the self-join with the left join example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions.

mysql> select
concat (d.department_id, ', ',  d.department_name) AS department,
concat (s. department_id, ', ', s. department_name) AS subject
FROM department d
LEFT JOIN department s
ON d.department_id = s. department_id
ORDER BY department DESC;

OUTPUT

SELF JOIN

The above output image shows the required columns of one table. The "department_name" and "department_id" columns contain in the department table with a different id.  The department and subject are two ids of the single table. The order by clause uses descending order for the department column.

5) Example: the self-join with the "WHERE" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. The "WHERE" clause uses for restriction and limitations on the table data.

mysql> select
concat (d.department_id, ', ',  d.department_name) AS department,
concat (s. department_id, ', ', s. department_name) AS subject
FROM department d
INNER JOIN department s
ON d.department_id = s. department_id
WHERE d.department_id < 5;

OUTPUT

SELF JOIN

This result displays the required columns of a single table. The "department_name" and "department_id" columns contain in the department table with a different id.  The department and subject are two ids of the single table. 6) Example: the self-join with the "LIMIT" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. The "LIMIT" clause uses for restriction and limitations on the table data.

mysql> select
concat (d.department_id, ', ',  d.department_name) AS department,
concat (s. department_id, ', ', s. department_name) AS subject
FROM department d
INNER JOIN department s
ON d.department_id = s. department_id
WHERE d.department_id < 5
LIMIT 2;

OUTPUT

SELF JOIN

The above output image shows the required columns of one table. The "department_name" and "department_id" columns show in the department table with a different id.  The department and subject are two ids of the single table. The "where" clause uses the "less than" five departments with two rows limit.


Related Topics

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.