ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What will be the SQL query to select the second highest salary from the employee table?

user-image
تم إضافة السؤال من قبل Tanmay Karmakar
تاريخ النشر: 2013/07/28
ADIL MUSTAFA
من قبل ADIL MUSTAFA , Senior System Engineer , Infosys Limited

You can use Rank() analytical function to achieve this.

the2 nd

SELECT max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary) FROM Employee)

OR

SELECT FROM Employee E1 WHERE (1) = SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2 WHERE E2.Salary > E1.Salary)

the N th

SELECT FROM Employee E1 WHERE (N-1) = SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2 WHERE E2.Salary > E1.Salary)

eslam Koraim
من قبل eslam Koraim

select  max(salary)

from     employees

where  salary <  (select max(salary) from employees)

Angelo Endaya
من قبل Angelo Endaya , Maximo Application Developer , IBM Solutions Delivery Inc.

SELECT max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary) FROM Employee);

yasser hassan
من قبل yasser hassan , Senior Oracle developer , zakah and Saudi Customs

select level,max(sal) from emp

where level =2

connect by prior (sal) > sal

group by level

Hassan Abdelwahab
من قبل Hassan Abdelwahab , Card Projects Specialist , Abu Dhabi Islamic Bank

select Max(Sal) from emp where Sal < (select Max(Sal) from emp)

Ali Mahdi Jawad
من قبل Ali Mahdi Jawad , Head of Information Technology , Falcon Cement Co

Select salary from

( select salary from employees order by salary desc )

Where rownum=2;

 

Please notice that rownum is pseudo column

select distinct sal from emp where sal = (select max(e.sal) from emp ewhere sal < (select max(sal) from emp ) ) order  by sal desc

select SALARY from EMPLOYEE EMP

where

2=(

select count(distinct SALARY) from EMPLOYEE

where

EMP.SALARY<=SALARY

);

 

This would work. You may give it a try...

Also you may replace2 with any number n to find nth highest salary.

Muhammad Kaleem ullah -
من قبل Muhammad Kaleem ullah - , Assistant Manager IT Development , Lahore Stock Exchange

<p>select E1.* from employees E inner join ( select E.empid,e.Salary, dens_rank over (order by Salary desc) as RNk from employee E ) T on E1.empid = T.empid where T.RNK =2</p>

Hassan Abazid
من قبل Hassan Abazid , IT Application Manager , Coca-Cola Yemen

SELECT * FROM hr.employees WHERE salary = (SELECT salary FROM (SELECT salary, ROW_NUMBER () OVER (ORDER BY salary DESC) r FROM hr.employees GROUP BY salary) WHERE r = :salary_rank)

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟