Hi,

Can anybody please explain me the flow of the below query.
I am not able to understand how this query works.

This query is for finding the Nth highest salary..

SELECT DISTINCT (a.sal)
FROM EMP A
WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B
WHERE a.sal<=b.sal);

If N = 2 then second highest salary will be the o/p.
If N = 3 then third highest salary will be the o/p.
and so on..

Thanks,
Nitin

Answer Posted / avi

Above Answer is correct With small modification that it is a
corelated sub query first it considers sal from a like a.sal
then it compares with all the sal in a sub query the result
will be the no. of counts.This count compares with N value
which matches will be the Nth sal.

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the popular database management systems in the it industry?

571


Can we insert in sql function?

551


what is data manipulation language? : Sql dba

533


What is the difference between microsoft access and sql?

500


What is the maximum database size for sql express?

540






What does <> sql mean?

542


What are different methods to trace the pl/sql code?

546


What is procedure and function?

520


What is database sql?

545


Explain foreign key in sql?

625


what is the stuff function and how does it differ from the replace function? : Sql dba

624


Is foreign key mandatory?

558


What is recursive stored procedure?

608


What is the difference between sql and isql*plus?

517


What is pl sql in oracle?

585