Find second lowest salary from emp table

Display the second lowest salary from emp table without using min & all

Questions by lalit.eng.kumar

Showing Answers 1 - 44 of 44 Answers

aleeza

  • May 7th, 2008
 

select min(sal) from emp where sal=(select sal from emp where sal<>MIN(SAL) ) ORselect min(sal) from emp where sal=(select sal from emp having sal<>MIN(SAL) ) May this will help you.I have not tested it in SQL and I am also not sure that the syntax is exactly right .may be you need to do same changes

  Was this answer useful?  Yes

aleeza

  • May 9th, 2008
 

select min(sal) from emp where sal=(select sal from emp where sal<>MIN(SAL) )            OR                                                                                                                                  select min(sal) from emp where sal=(select sal from emp having sal<>MIN(SAL) )                                                                                                                                                 May this will help you.I have not tested it in SQL and I am also not sure that the syntax is exactly right .may be you need to do same changes
 

select min(sal) from emp where emp.empcode in(select top 2 empcode from emp order by sal desc)

Go with this query (use inMS SQL server database)
Let me know, it is right or wrong.

  Was this answer useful?  Yes

yogesh.raut

  • Mar 13th, 2010
 

SELECT * FROM ( SELECT rownum n, id, salary FROM (select id, salary from employees order by salary )) where n = 2

This works perfectly even if you can find any nth highest or lowest salary.

  Was this answer useful?  Yes

kirandba

  • Mar 31st, 2010
 

Try this

SELECT min(sal) FROM emp WHERE sal > (SELECT Min(sal) FROM emp)

Inner query returns lowest salary. Then get lowest salary excluding the first lowest salary.

tapasgaan

  • Nov 9th, 2010
 

Select e.salary  from emp e

where 1= ( select count(*) from emp s
                  
                  where s.salary<e.salary)

  Was this answer useful?  Yes

 there r various query for this question

1  select min(salary)
      from emp
      where salary in ( select top 2 salary
                                   from emp
                                    order by salary desc);

mkaleelmd

  • Apr 7th, 2011
 

SELECT * FROM

            (SELECT
                  employee_id
                  ,salary
                 ,dense_rank() OVER ( ORDER BY salary) as ranking
             FROM employees) a
WHERE a.ranking=2

Select * From (Select * From sample_count_ex Where salary < (Select salary From (Select * From sample_count_ex Order By 4 Desc) Where rownum = 1) Order By 4 Desc) Where rownum = 1;

First query Select * From sample_count_ex Order By 4 Desc gets salary in descending order as we r doing order by desc;

So (Select salary From (Select * From sample_count_ex Order By 4 Desc) Where rownum = 1) gets Highest salary in that table

and (Select * From sample_count_ex Where salary < (Select salary From (Select * From sample_count_ex Order By 4 Desc) Where rownum = 1) Order By 4 Desc) gets Second highest Salary

and since rownum= 1
 
it returns only Second highest salary every time

  Was this answer useful?  Yes

ziaur10

  • Jun 1st, 2011
 

Sorry Previous is not good but this one seems good.


Thanks 
Ziaur
---------------------------------------------------------------------------------------

--USE [MyDb]
GO
-- Clear  all
drop table [dbo].[Student]
GO
/****** Object:  Table [dbo].[Student]    Script Date: 06/01/2011 18:40:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[Name] [varchar](255) NULL,
[Number] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's1', 57)
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's2', 60)
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's3', 75)
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's4', 85)
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's5', 60)
INSERT [dbo].[Student] ([Name], [Number]) VALUES (N's6', 55)

-------------------------------------------------------------
declare  @nth int 
set @nth = 2
select * from (
               select Name, Number, dense_rank() OVER (order by number ) AS 'position'
          from Student ) as s
where  s.position =  @nth

  Was this answer useful?  Yes

yeomanie

  • Jul 4th, 2011
 

Select Salary From Empwhere Salary In(Select Salary From Emporder By Salary Asc where Rownum = 2);

  Was this answer useful?  Yes

Ravindra Anugade

  • Aug 17th, 2011
 

-- Find Second lowest Salary ----

SELECT DISTINCT(a.sal) FROM emp a WHERE 2 = ( SELECT DISTINCT(COUNT(b.sal)) FROM emp b
WHERE a.sal >= b.sal )

  Was this answer useful?  Yes

Little Foot

  • Aug 19th, 2011
 

Code
  1.  

  Was this answer useful?  Yes

Niraj

  • Mar 22nd, 2012
 

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

  Was this answer useful?  Yes

ram

  • Apr 16th, 2012
 

Code
  1.  

  Was this answer useful?  Yes

Sudeep Mohan Nayak

  • Oct 12th, 2017
 

Code
  1.  

  Was this answer useful?  Yes

satyam singh

  • Apr 22nd, 2020
 

"HAVING" clause is required. It works with "GROUP BY" clause

  Was this answer useful?  Yes

Pendo

  • Jul 17th, 2021
 

SELECT Top (1) Salary FROM Employee
ORDER BY Salary

Code
  1.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions