Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle SQL Iterate through each item

I have a simple requirement that aims to achieve the following:

 Resulting_column_1     Resulting_column_2
 Apple                  Orange
 Apple                  Banana
 Apple                  Cherry
 Orange                 Apple
 Orange                 Banana
 Orange                 Cherry
 Banana                 Apple
 Banana                 Orange
 Banana                 Cherry
 Cherry                 Apple
 Cherry                 Banana
 Cherry                 Orange

This is my table:

CREATE TABLE "FRUITS" 
(   "FRUIT_NAME" VARCHAR2(100)
) ;

insert into fruits (fruit_name) values ('Apple')
insert into fruits (fruit_name) values ('Banana')
insert into fruits (fruit_name) values ('Cherry')
insert into fruits (fruit_name) values ('Orange')

Anyone knows how to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

Join the table to itself where the names are not equal to each other:

select f1.fruit_name f1, f2.fruit_name f2
from   fruits f1
join   fruits f2
on     f1.fruit_name <> f2.fruit_name;

F1        F2        
Apple     Banana    
Apple     Cherry    
Apple     Orange    
Banana    Apple     
Banana    Cherry    
Banana    Orange    
Cherry    Apple     
Cherry    Banana    
Cherry    Orange    
Orange    Apple     
Orange    Banana    
Orange    Cherry
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

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

Continue reading