1

I would like to find out a PostgreSQL command that reads the content of a table, and returns all the possible couplings of the primary key of the table.

Something like this:

input_table (ID is the primary key):

- - - - - - - - - - 
ID | label | name
- - - - - - - - - - 
11 | aaa | Jenny

24 | bbb | Larry

35 | ccc | Trevor

48 | ddd | Jules
- - - - - - - - - - 

The query should return all the possible couplings of the primary keys:

- - - - - - - - - - 
ID_left | ID_right
- - - - - - - - - - 
11 | 24

11 | 35

11 | 48

24 | 35

24 | 48

35 | 48
- - - - - - - - - - 

Is it possible to design this query? If yes, how?

Thanks

1 Answer 1

6

Sounds like a simple cross join:

select a.id, b.id
from input_table a
  cross join input_table b
where a.id <> b.id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.