DB2 SQL: How to Write Self-join example

Why Do you need it? To refer to fields within the same table you can go for self-join. Below is the example SQL query for self-join.

Self-Join SQL Query

Let us first create two copies of the same table MATCH, M1 and M2:

CREATE TABLE M1 AS SELECT * FROM MATCH;

CREATE TABLE M2 AS SELECT * FROM MATCH;

Then, we could join M1 and M2:

SELECT M1.PAPER FROM M1, M2 
WHERE M1.PAPER = M2.PAPER  
AND M1.CODE = 601 AND M2.CODE = 602;

The self-join allows us to perform this sort of operation without actually having to copy the table. We can just act as if we had two copies.

Related

Discover more from Srinimf

Subscribe now to keep reading and get access to the full archive.

Continue reading