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 is foreign key in sql with example?

504


how to convert numeric values to character strings? : Sql dba

564


GLOBAL TEMPORARY TABLE over Views in advantages insolving mutating error?

2563


Can %notfound return null after a fetch?

579


Is sqlite free?

525






How do you create a db file?

512


Do triggers have restrictions on the usage of large datatypes, such as long and long raw?

676


What is time based sql injection?

480


Why is stored procedure faster than query?

482


What are the possible values that can be stored in a boolean data field?

507


What is difference between pls_integer and integer?

512


What does where 1/2 mean in sql?

521


Can you join a table to itself?

532


What is cursor and its types?

548


What is dml statement?

509