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

How do you modify a trigger?

524


Explain the structure of pl/sql in brief.

619


How do I remove all records from a table?

564


what is an index? : Sql dba

525


How can I get the number of records affected by a stored procedure?

574






What are few of the schema objects that are created using PL/SQL?

561


how to get a list of all tables in a database? : Sql dba

512


what is a primary key? : Sql dba

554


Does view store data in sql?

532


Is subquery faster than join?

569


What is bind variable in pl sql?

527


how to rename an existing table in mysql? : Sql dba

575


what are the 'mysql' command line arguments? : Sql dba

651


How many sectors are in a partition?

563


how to get @@error and @@rowcount at the same time? : Sql dba

527