MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL SELF JOIN

In MySQL, SELF JOIN is used to combine rows with other rows in the same table. In other words table is joined with itself. This can be achive with the help of table alias to distinguish the left table from the right table of the same table in a single query.

MySQL SELF JOIN Syntax

To combine rows with other rows in the same table, use the following syntax:

Syntax

SELECT col1, col2,....
FROM tablename1 A, tablename1 B
WHERE condition;

Parameters:

tablename1: The table name from which you want to fetch records.

col1: The column or field name that will be return.

MySQL SELF JOIN example

The following MySQL, combining rows with other rows in the same table using SELF JOIN:

Example

SELECT A.firstName, A.lastName, B.firstName, B.lastName
FROM tblCustomerLoan A, tblCustomer B
WHERE A.firstName <> B.firstName;

Note:

In the above example, we are combining rows with other rows in the same table using SELF JOIN.

You can use MySQL Command Line Client to combine rows in the same table.