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