SQL Interview Questions - Second hightest Salary

Problem Statement: Find the second highest salary of an employee

Table structure 

 create table employee

(id int, name varchar(20), salary int);


insert into employee (id, name,salary) values(1,'Shad',2000);

insert into employee (id, name,salary) values(2, 'Arif', 1500);

insert into employee (id, name,salary) values(3, 'Sarif', 1400);

insert into employee (id, name,salary) values(4,'Marif', 1300);

insert into employee (id, name,salary) values(5, 'Saleem',1999);

insert into employee (id, name,salary) values(6, 'tarif', 1200);

insert into employee (id, name,salary) values(7, 'jarif', 1100);

insert into employee (id, name,salary) values(8, 'Darif',1000);


Solutions:

select distinct salary from employee e1 

where 2= ( select count(distinct salary) from employee e2   where e1.salary<=e2.salary )


 

Comments

Popular posts from this blog

SQL Interview Questions - SqlWorldCup