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...



How to retrieve a second highest salary from a table?
Note:Suppose salaries are in duplicate values
eg:
Name Sal
Malli 60000
Pandi 60000
Rudra 45000
Ravi 45000

Answers were Sorted based on User's Feedback



How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / mallinathabj

The Following queries to retrieve the second highest salary

SQL> select max(sal) from emp where sal<(select
max(distinct(sal)) from emp);

SQL> select min(sal) from(select distinct( sal) from emp order
by sal desc) where rownum<=2;

SQL> SELECT MAX(SAL) FROM EMP WHERE SAL NOT IN (SELECT
MAX(SAL) FROM EMP);

Is This Answer Correct ?    13 Yes 2 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / bibu

This IS HELP FULL 1ST ,2ND,3RD,4TH ANY HIGHEST SALARY U WRITE THIS QUERY.

SELECT * FROM Table_Name ALIAS_NAME 1
WHERE &N=(
SELECT COUNT(DISTINCT(WHICH COL U WANT THAT COL_NAME))
FROM Table_Name ALIAS_NAME 2
WHERE ALIAS1.CoL<=ALIAS2.Col
)

EX:
SELECT * FROM Emp E1
WHERE &N=(SELECT COUNT(DISTINCT(Sal))
FROM Emp E2
WHERE E1.Sal<=E2.Sal)

Is This Answer Correct ?    11 Yes 0 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / prativa mishra

select *
from
(select salary,dense_rank() over (order by salary) kk from table_name)
where kk=2

Is This Answer Correct ?    7 Yes 2 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / praveenpinfo

Suppose table name is EMP.

SQL> Select distinct sal from emp e1 where 2=(select
count(distinct sal) from emp e2 where e1.sal<=e2.sal);

Is This Answer Correct ?    3 Yes 0 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / ashok

select sal from emp order by sal desc offset 1 rows fetch next 1 rows with ties;

Is This Answer Correct ?    1 Yes 0 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / shriram

You can also do it by the following query ..

select * from (select name,salary,rank() over(order by
salary desc as r) from employee) where r = 2;

The above query returns the 2nd highest salary from the table.

Is This Answer Correct ?    3 Yes 3 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / dewesh

With cte as (
Select *, Dense_rank() over (order by salary desc) as s from emp)
Select EmpName, Salary from cte where s =2
It has work. But duplicate value is not removed if name is different

Is This Answer Correct ?    0 Yes 0 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / welcomeashwin

--2 ND HIGHEST SALARY
SELECT MAX(SALARY) FROM HR.EMPLOYEES
WHERE SALARY < (SELECT MAX(DISTINCT(SALARY)) FROM
HR.EMPLOYEES);

--N TH HIGHEST SALARY
SELECT * FROM HR.EMPLOYEES EMP1
WHERE &N=(
SELECT COUNT(DISTINCT(SALARY))
FROM HR.EMPLOYEES EMP2
WHERE EMP1.SALARY<=EMP2.SALARY
);

Is This Answer Correct ?    0 Yes 1 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / sreeharibabu

SELECT id
FROM (select salary2.*, rownum rnum from
(select * from test ORDER BY id DESC) salary2
where rownum <=2 )
WHERE rnum >= 2;

Is This Answer Correct ?    0 Yes 1 No

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate valu..

Answer / rajat

SELECT * FROM (
SELECT RANK()OVER( ORDER BY SALARY DESC) NUM, A.* FROM RAJ A)
WHERE NUM=2;

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL PLSQL Interview Questions

How do I get sql certification?

0 Answers  


What if we write return in procedure?

0 Answers  


how can stop the sequence with mention the max value and with out mention the max value

1 Answers   Zensar,


What does partition by mean in sql?

0 Answers  


Write a query to get 2nd maximum salary in an employee table ?

69 Answers   Accenture, BirlaSoft, Letse, Logica CMG, Qwest, Rheal Software, Saagam, Semantic Space, Tailor Solution, TCS, TinyERP,


what is a trigger? : Sql dba

0 Answers  


How do u count no of rows in a table?

8 Answers   CTS,


Can we use having without group by in sql?

0 Answers  


What is a procedure in pl sql?

0 Answers  


How to convert ms-excel file to oracle table?

2 Answers  


what is the use of double ampersand (&&) in sql queries?

0 Answers  


What is an inner join sql?

0 Answers  


Categories