Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Explain correlated query work?

1054


what is an execution plan? When would you use it? How would you view the execution plan? : Sql dba

978


What is the difference between in and between in sql?

1061


what are the advantages a stored procedure? : Sql dba

976


what is meant by urlencode and urldocode? : Sql dba

1176


how to create a new table in mysql? : Sql dba

986


What does select * from mean in sql?

3238


What is rank () in sql?

941


Why do we use partitions in sql?

1048


What is the difference between rollback and rollback to statements?

1060


how many columns can be used for creating index? : Sql dba

946


what is online transaction processing (oltp)? : Sql dba

994


what is cursor. write example of it. What are the attributes of cursor.

1391


how can we find the number of rows in a table using mysql? : Sql dba

1083


What are the different types of dbmss?

1015