Question

I have several question about self join, could anyone help answer it?

  1. is there strict format of self join? There are sample like this:

    SELECT a.column_name, b.column_name...
    FROM table1 a, table1 b
    WHERE a.common_field = b.common_field;
    

But there are sample like:

    SELECT  a.ID, b.NAME, a.SALARY
    FROM CUSTOMERS a, CUSTOMERS b
    WHERE a.SALARY < b.SALARY;

I wonder is the connection (a.common_field = b.common_field) necessary? since both formats are self join.

How will the self join be optimized? will they are treated as INNER JOIN or CROSS JOIN? especially, for the second format, is it SELF CROSS JOIN? In SQLite and PostgreSQL, are they treated same way?

My question is I want to extract a structure from a bunch of graph-like data and My query is like

    SELECT A.colum, B.colum,....N.colum
    FROM 
    table1 as A,  table1 as B, table1 as C .... table2 as M, table2 as N .... 
    where 
    A.colum1<B.colum1 and 
    C.colum1=D.colum1 and 
    ....

In the query, table1,table2... are single column tables, they are components of final structure. is my problem best in this kind of self-join format? I find it's very slow in PostgreSQL but fast in SQLite which makes me confused.

Was it helpful?

Solution

A self join is no different than any other join as far as structure/behavior goes, but they are typically used in different ways.

You should ditch the deprecated syntax of comma separated lists of tables and use ANSI joins:

SELECT a.column_name, b.column_name...
FROM table1 a
JOIN table1 b
 ON a.common_field = b.common_field;

You can specify what type of JOIN you want it to be (JOIN,LEFT JOIN, RIGHT JOIN,CROSS JOIN..), and how you want to relate the tables to each other, just like any other join. Equivalency is not required, as you've noted in your a.Salary < b.Salary example.

OTHER TIPS

No, there's no such thing.

A self join is just a special case of joining the table with itself. Think about it like joining two instances of the same thing (is fact no using two instances but two references)

In general you ill inner self join but you can cross join or outter join a table with itself.

Example:

select * from tbPeople p0
join tbPeople p1 on p1.id = p0.parentId
where p0.id = you

that returns you and your parents

select * from tbPeople p0
left join tbPeople p1 on p1.parentId = p0.id
where p0.id = you

that returns your kids, or just you in case you don't have offspring yet

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top