...

Database Managment - MySql

Back to Course

Lesson Description


Lession - #1339 SQL Self Join



SELF JOIN: 

Consider the two tables below:
Student
Sorry cannot be displayed

StudentCourse
Sorry cannot be displayed


A table is joined to itself with SELF JOIN, as the name implies. That is, depending on certain criteria, each table row is linked to itself and all other rows. To put it another way, it's a join between two copies of the same table.

Syntax:

SELECT a.coulmn1 , b.column2
FROM table_name a, table_name b
WHERE some_condition;

table_name: Name of the table.
some_condition: Condition for selecting the rows.
Example Queries(SELF JOIN>
:


The names and ages of students registered in various classes will be displayed using this query.

SELECT a.ROLL_NO , b.NAME
FROM Student a, Student b
WHERE a.ROLL_NO < b.ROLL_NO;


Output:

Sorry cannot be displayed