×

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 CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL DAY() function

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

2 minutes read.

MySQL PI() function

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

2 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 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.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 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 LIKE() function

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

4 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 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 FORMAT() function

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

2 minutes read.