Write a query to display all possible paths given source

 Input:

 Output:



drop table t_paths;

create table t_paths (id number,name varchar2(10));

insert into t_paths values(1,'USA');

insert into t_paths values(2,'EUROPE');

insert into t_paths values(3,'ASIA');

insert into t_paths values(4,'AUSTRALIA');

COMMIT;

Solution1:

select a.name col1,b.name col2 from t_paths a,t_paths b

      where a.id<b.id

Output:

USA       EUROPE
USA       ASIA
USA       AUSTRALIA
EUROPE    ASIA
EUROPE    AUSTRALIA
ASIA      AUSTRALIA


Solution2:

select a.name col1,b.name col2 from t_paths a,t_paths b

where a.id>b.id;

 Output:

AUSTRALIA    ASIA
AUSTRALIA    EUROPE
AUSTRALIA    USA
ASIA         EUROPE
ASIA         USA
EUROPE       USA

Solution 3:

select

distinct greatest(a.name,b.name) col1,

least(a.name,b.name) col2 from t_paths a,t_paths b

where a.name <> b.name

order by col1 desc;

Output:

USA        ASIA
USA        AUSTRALIA
USA        EUROPE
EUROPE     ASIA
EUROPE     AUSTRALIA
AUSTRALIA  ASIA

 


No comments:

Post a Comment