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

Answers were Sorted based on User's Feedback



Hi, Can anybody please explain me the flow of the below query. I am not able to understand how t..

Answer / satyajit patel

The Query above gives the nth highest distinct salary.

For explanation let a table emp with sal column like below
sal
1200
1300
1500
1200
1250
1700
1250
2000

See DISTINCT word is there in the query
So, you have to find the distinct sal first.
sal
1200
1300
1500
1250
1700
2000
Now see the condition a.sal<= b.sal

This condition compares a.sal and b.sal. The COUNT counts
how many times the a.sal is less than or equal to b.sal,
and gives this value as the output of sub query.
(N.B. comparing to same value means EQUAL SO count is 1).
This count is the value of N.

So after comparision the value of N for different salaries
are like
sal N
1200 6
1300 4
1500 3
1250 5
1700 2
2000 1

Now on querying when you give a value of N the
corresponding value of salary is selected.
Like if you are giving N=2 , then 1700 is displayed.

Is This Answer Correct ?    28 Yes 0 No

Hi, Can anybody please explain me the flow of the below query. I am not able to understand how t..

Answer / 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

Hi, Can anybody please explain me the flow of the below query. I am not able to understand how t..

Answer / ms75

Detailed answer available on following weblinks
-----------------------------------------------

1) How does this query work?

<http://www.sqlteam.com/article/find-nth-maximum-value-in-
sql-server>

2) How this query works?

<http://www.dbapool.com/forumthread/topic_4361.html>

Is This Answer Correct ?    1 Yes 0 No

Hi, Can anybody please explain me the flow of the below query. I am not able to understand how t..

Answer / nitin

Thank you.

Is This Answer Correct ?    2 Yes 1 No

Hi, Can anybody please explain me the flow of the below query. I am not able to understand how t..

Answer / mandar

HI, this querey is showing the output for the employees
salary. for the which employees salary is highest in that
table this query showing the output.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL PLSQL Interview Questions

Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ?

5 Answers  


Will truncate release space?

0 Answers  


can use the following like overloading concept in a single package: procedure p1(a varchar), procedure p1(a varchar2), procedure p1(a char)

5 Answers  


What are the sql commands?

0 Answers  


explain what is mysql? : Sql dba

0 Answers  


ERROR:Insert or update on table"accnt" violates foreign key constraints "acct_to_curr_symbol" DETAILS:KEY(accnt_curr_id)(-2)is not present in the table "curr_symbol" ......solve The Problem..

0 Answers   Wipro,


How to count the no of records of a table without using COUNT function?

11 Answers   TCS, Tesco,


Is sqlexception checked or unchecked?

0 Answers  


What are the steps you take to tune(performance tuning) the code in plsql?

4 Answers   Cap Gemini, Infosys, TCS,


Do you understand the meaning of exception handling?

3 Answers  


What is the need of merge statement?

0 Answers  


What is procedure explain with example?

0 Answers  


Categories