find out the third highest salary?
Answers were Sorted based on User's Feedback
Answer / rajdevar
Guys
i have executed this query in sql plus.This is correct
Select * from EMP_USER A where n-1 = (select count
(distinct (sal)) from EMP_USER B where A.sal<B.sal)
where n=3
reason for using n-1 is below:
1.We are using correlated sub query.so sal value from each
row in outer query(EMP_USER A) is compared with sal of all
the rows in EMP_USER B
eg:
ENAME SAL
----- -----
SMITH 800
KING 5000
FORD 3000
here third highest is 800.so when executing this 800 from
outer query is compared with 800,5000,3000 in inner query
which returns a count(distinct(sal) = 2
if you use n instead of n-1 you get a empty result.
Let me know if this is correct
Is This Answer Correct ? | 13 Yes | 5 No |
Answer / vishal
Here is the correct query:
select ename,sal from emp e
where 3 = (select count(distinct(sal))+1 from emp
where e.sal < sal);
Is This Answer Correct ? | 12 Yes | 5 No |
Answer / sudheer
Guys Few of you have submitted an optimal query.
select distinct sal from emp A where 3 = (select count(sal)
from emp B where a.sal > b.sal)
However the integer 3 will not give the third highest salary
as expected. Point should be noted that the row starts with
zero.
so the actual result will the fourth highest salary with the
above query.
Is This Answer Correct ? | 7 Yes | 3 No |
Answer / kss
Hi every one,
i know that we can get that thr above quer..
one can try using analytic function giving ranks to the
salary column
select sal,dense_rank() over(order by sal desc) rank from
emp
Is This Answer Correct ? | 18 Yes | 15 No |
Answer / nehal
select e.sal from employee where n-1=(Select count(distinct
sal) from emp where sal>e.sal)
Put n=3 for the 3rd highest salary.
Is This Answer Correct ? | 10 Yes | 7 No |
Answer / selvaraj anna university coe
In Oracle 9i:
-------------
SQL> SELECT MAX(salary) FROM emp WHERE LEVEL=&no CONNECT BY
PRIOR Salary>salary;
Enter value for no: 3
old 1: SELECT MAX(SALARY) FROM EMP WHERE LEVEL=&no
CONNECT BY PRIOR SALARY>SALARY
new 1: SELECT MAX(SALARY) FROM EMP WHERE LEVEL=3 CONNECT
BY PRIOR SALARY>SALARY
MAX(SALARY)
-----------
500000
SQL>
Is This Answer Correct ? | 8 Yes | 5 No |
Answer / chanchal
SELECT MIN(salary) FROM Department WHERE salary IN
(SELECT TOP 3 salary FROM Department ORDER BY salary DESC)
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / raj
Answer 28 is the dumbest way of writing the query.what
would you write if i want the 25 highest salary? repeat
this satement 25 times.
Is This Answer Correct ? | 3 Yes | 1 No |
can a table has a column that has only view data and in other columns we can change data?
Is the After report trigger fired if the report execution fails ?
Can a formula column be obtained through a select statement ?
Select all the employees who were hired in last 2 years and who works in dept where max managers are working.
a query to select maxmun 3 salaries of employee table
What is a database table in oracle?
Please explain oracle data types with examples?
How to do clean up if create database failed?
how to get the second max val for every group in a table
Can we create more than one constraint to column ?
Difference between primary key and unique key ?
77 Answers Accenture, B2B Software Technologies, Cognizant, HP Finsoft, IndiaNIC, Karomi Technology, Keane India Ltd, L&T, Onward eServices, R Systems, Shakti, Techtic Solutions,
How can you merge two tables in oracle?