GeekInterview.com
Series: Subject: Topic:
Question: 54 of 214

Find second lowest salary from emp table

display the second lowest salary from emp table without using min & all
Asked by: lalit.eng.kumar | Member Since Apr-2008 | Asked on: May 1st, 2008

View all questions by lalit.eng.kumar

Showing Answers 1 - 41 of 41 Answers
aleeza

Answered On : May 7th, 2008

View all answers by aleeza

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

  
Login to rate this answer.
aleeza

Answered On : May 9th, 2008

View all answers by aleeza

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
 

Yes  1 User has rated as useful.
  
Login to rate this answer.
ashjain78

Answered On : May 22nd, 2008

View all answers by ashjain78

SELECT a.Emp_Name,a.Emp_sal FROM employee a where 2=(SELECT count(distinct(b.Emp_sal)) FROM employee b WHERE a.Emp_sal<=b.Emp_sal)

Yes  1 User has rated as useful.
  
Login to rate this answer.

dont make simple queries the big ones,try to use as simple as possible:-

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

Yes  1 User has rated as useful.
  
Login to rate this answer.

dont use that that is for second highest salary

  
Login to rate this answer.

sel emp_no,rank(emp_no) from emp order by acct_no asc QUALIFY RANK(emp_no) =2

  
Login to rate this answer.

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.

  
Login to rate this answer.
ashwin890

Answered On : Jul 11th, 2008

View all answers by ashwin890

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
anu1985

Answered On : Jul 17th, 2008

View all answers by anu1985

select salary ,empname from emptable order by(salary) limit 1,1;

  
Login to rate this answer.
maartha

Answered On : Jul 21st, 2008

View all answers by maartha

SELECT esalary FROM emp ORDER BY `esalary ` ASC LIMIT 1,1

  
Login to rate this answer.
groovypash

Answered On : Sep 28th, 2008

View all answers by groovypash

select max(salary) from dept where salary in(select distinct top 2  salary from dept order by salary)

Tested query. Change top 2 to top 3 for 3 rd lowest and so on.

Yes  1 User has rated as useful.
  
Login to rate this answer.
ashcarter

Answered On : Feb 5th, 2009

View all answers by ashcarter

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

  
Login to rate this answer.
luzaolai

Answered On : Apr 26th, 2009

View all answers by luzaolai

With t1 as (select firstnme, salary, rank()over(order by salary) as rank from employee)
select * from t1 where rank=2

Yes  1 User has rated as useful.
  
Login to rate this answer.

select min(salary) from emp where salary!=(select min(salary) from emp)

Try this it's 100% true answer

  
Login to rate this answer.
yogesh.raut

Answered On : Mar 13th, 2010

View all answers by yogesh.raut

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.

  
Login to rate this answer.
kirandba

Answered On : Mar 31st, 2010

View all answers by kirandba

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.

select * from (select rownum rank,salary from (select distinct(salary) from employees order by salary asc)) where rank = &n;

Place whatever the number that you want in place of N.

Yes  1 User has rated as useful.
  
Login to rate this answer.
pravintule

Answered On : May 28th, 2010

View all answers by pravintule

select min(sal) from (select sal from employee where sal>(select min(sal) from employee))

Yes  1 User has rated as useful.
  
Login to rate this answer.
nisargkothari

Answered On : Sep 16th, 2010

View all answers by nisargkothari

SELECT TOP 1 Empname,Salary FROM (SELECT TOP 2 Empname,Salary FROM Employee ORDER BY Salary ASC) X ORDER BY Salary DESC

  
Login to rate this answer.
ngiridhar903

Answered On : Oct 22nd, 2010

View all answers by ngiridhar903

select * from scott.emp a where 2 = ( select count(distinct(b.sal)) from scott.emp b where a.sal>=b.sal)

  
Login to rate this answer.
tapasgaan

Answered On : Nov 9th, 2010

View all answers by tapasgaan

Select e.salary  from emp e

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

  
Login to rate this answer.

 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);

Yes  1 User has rated as useful.
  
Login to rate this answer.
mkaleelmd

Answered On : Apr 7th, 2011

View all answers by mkaleelmd

SELECT * FROM

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
jackiezhou

Answered On : Apr 12th, 2011

View all answers by jackiezhou

select top(1) Sales
from dbo.Sales
where EmpId <> (
select top(1) EmpId from dbo.Sales order by Sales 
)
order by Sales 

  
Login to rate this answer.
abhishekrc2007

Answered On : Apr 16th, 2011

View all answers by abhishekrc2007

select salary from emp_table order by salary asc limit 1,1;

  
Login to rate this answer.
Pallavi g rao

Answered On : Apr 21st, 2011

View all answers by Pallavi g rao

select * from emp  a where 2=(select count(distinct(b.sal))from emp b where a.sal>=b.sal)

  
Login to rate this answer.
chaitanya_m03

Answered On : May 19th, 2011

View all answers by chaitanya_m03

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

  
Login to rate this answer.
mukes72

Answered On : May 27th, 2011

View all answers by mukes72

select min(salary) from emp_table<>(select min(salary) from emp_table
);

  
Login to rate this answer.
ziaur10

Answered On : Jun 1st, 2011

View all answers by ziaur10

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

  
Login to rate this answer.
Shaik Kareem

Answered On : Jun 6th, 2011

View all answers by Shaik Kareem

select min(sal) from emp where sal>(select min(sal) from emp)

Yes  1 User has rated as useful.
  
Login to rate this answer.
yuvaevergreen

Answered On : Jun 8th, 2011

View all answers by yuvaevergreen

sel min(salary) from
(sel salary,rank() over
(order by salary asc) rnk
 from emp) a
 where a.rnk>1; 

  
Login to rate this answer.

select sal from(select sal,rank() over (order by sal asc) as rank from emp ) where rank=2;

  
Login to rate this answer.
yeomanie

Answered On : Jul 4th, 2011

View all answers by yeomanie

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

  
Login to rate this answer.

SELECT sal FROM 

 (SELECT sal FROM emp ORDER BY sal DESC)
               WHERE rownum=2;

  
Login to rate this answer.
Samanta

Answered On : Aug 11th, 2011

View all answers by Samanta

Select Sal from
(Select sal, rank() over(order by sal desc)rnk
from employee)temp where rnk=2

  
Login to rate this answer.
Ravindra Anugade

Answered On : 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 )

  
Login to rate this answer.
Little Foot

Answered On : Aug 19th, 2011

Code
  1. SELECT * FROM (SELECT * FROM (SELECT DISTINCT sal FROM emp ORDER BY sal DESC) WHERE ROWNUM < n+1 ORDER BY sal) WHERE rownum<2

  
Login to rate this answer.

Code
  1. SELECT MIN(SAL) FROM EMPLOYEE WHERE SAL>(SELECT MIN(SAL) FROM EMPLOYEE)

  
Login to rate this answer.
Niraj

Answered On : Mar 22nd, 2012

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

  
Login to rate this answer.
ram

Answered On : Apr 16th, 2012

Code
  1. SELECT * FROM emp a WHERE (&n-1) =(SELECT count(UNIQUE sal) FROM emp b WHERE a.sal>b.sal) FOR nth minimum salary
  2. SELECT * FROM emp a WHERE (&n-1) =(SELECT count(UNIQUE sal) FROM emp b WHERE a.sal<b.sal) FOR nth maximum salary

  
Login to rate this answer.

Ypu need to know MR. SQL that you cant use = operator with rownum ....

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.