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 server name sql?

0 Answers  


Which query operators in sql is used for pattern matching?

0 Answers  


how to check the 3rd max salary from an employee table? One of the queries used is as follows: select sal from emp a where 3=(select count(distinct(sal)) from emp b where a.sal<=b.sal). Here in the sub query "select count(distinct(sal)) from emp b where a.sal<=b.sal" or "select count(distinct(sal)) from emp b where a.sal=b.sal" should reveal the same number of rows is in't it? Can any one here please explain me how is this query working perfectly. However, there is another query to get the 3rd highest of salaries of employees that logic I can understand. Pls find the query below. "select min(salary) from emp where salary in(select distinct top 3 salary from emp order by salary desc)" Please explain me how "select sal from emp a where 3=(select count(distinct(sal)) from emp b where a.sal<=b.sal)" works source:http://www.allinterview.com/showanswers/33264.html. Thanks in advance Regards, Karthik.

4 Answers  


What is difference between Procedures and Functions ?

6 Answers   Hi Caliber IT,


What is nested table in pl sql?

0 Answers  






Can we insert data into view?

0 Answers  


Mention what plvcmt and plvrb does in pl/sql?

0 Answers  


Can a table have two primary keys?

44 Answers   HCL, MetaCube,


what is offset-fetch filter in tsql? : Transact sql

0 Answers  


How are functions and procedures called in PL/SQL?

0 Answers  


Why do we create stored procedures & functions in pl/sql and how are they different?

0 Answers  


Can we use view in stored procedure?

0 Answers  


Categories