source destination distance
chennai bangalore 500
bangalore chennai 500
hyd delhi 1000
delhi hyd 1000
bangalore hyd 800

Here chennai to bangalore and bangalore to chennai is same distance. and hyd and delhi also same criteria.
Based on the distance i want to display only one row using sql query?

Answers were Sorted based on User's Feedback



source destination distance chennai bangalore 500 bangalore chennai 500 hyd delhi 1000..

Answer / nihar meher

select distinct greatest(source,destination),least(source,destination) from tablename;

Is This Answer Correct ?    3 Yes 1 No

source destination distance chennai bangalore 500 bangalore chennai 500 hyd delhi 1000..

Answer / ajitnayak

select distinct source,destination,distance from dist_tab where source > destination
minus
select distinct source,destination,distance from dist_tab where source < destination

Is This Answer Correct ?    0 Yes 0 No

source destination distance chennai bangalore 500 bangalore chennai 500 hyd delhi 1000..

Answer / sushma ajay

select source, destination, distance from(
select source,destination,lag(source) over (order by distance) oldsrc,distance
from traveldist)
where nvl(destination,'@') <> nvl(oldsrc,'@');

Is This Answer Correct ?    5 Yes 6 No

source destination distance chennai bangalore 500 bangalore chennai 500 hyd delhi 1000..

Answer / deepak

select source, destination, distance from Distance_rcd where source>destination
union select destination, source, distance from Distance_rcd where source<destination

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More SQL PLSQL Interview Questions

What is a trigger in sql?

0 Answers  


what is mean by forward declaration and where we'll use it.

4 Answers   TCS,


Cite the differences between execution of triggers and stored procedures?

0 Answers  


How does a trigger work?

0 Answers  


What is a data definition language?

0 Answers  






Can we use views in stored procedure?

0 Answers  


How many sql statements are used? Define them.

0 Answers  


Fetch an entire row from the employees table for a specific employee ID:

2 Answers  


How collections are improve the performance?

1 Answers   Polaris,


How does cross join work?

0 Answers  


Does pl sql work in mysql?

0 Answers  


Explain the difference between rename and alias?

0 Answers  


Categories