Friday, January 1, 2021

How to Find the Third highest salary in mysql

 


set @b:=0;
set @b1:=3;   #
#select distinct salary,@b:=@b+1 as Row_no from emp
select  * from emp where salary=(
select salary from (
select a.salary,@b:=@b+1 as row_no from (
select distinct salary from emp
) a) b
where b.row_no=@b1


 

 b).How to find the duplicate value in mysql

 select Name,count(*) from emp  
 group by Name
 having count(*)>1 ;
 


Q. Print the name of employees having the highest salary in each department.

select *from table1 a
where a.salary in (
select  max(b.salary) salary from table1 b group by b.dept_name
)



Q. Fetch three max salaries from a table.

select * from table1 a
where 3 >
(
select count(*) from table1 b where a.salary < b.salary
)

 

Q.Fetch three min salaries from a table.

select distinct convert(a.salary,unsigned) as salary from table1 a
where 3 >=
(
select count(distinct convert( b.salary,unsigned)) from table1 b
where convert(a.salary,unsigned) >= convert(b.salary,unsigned)
)

Q. Write an SQL query to fetch departments along with the total salaries paid for each of them.

select dept_name,sum(convert(a.salary,unsigned)) as salary from table1 a
group by dept_name


 



 
 
 
 
 
 


No comments:

Post a Comment

How to Find the Third highest salary in mysql

  set @b:=0; set @b1:=3;   # #select distinct salary,@b:=@b+1 as Row_no from emp select  * from emp where salary=( select salary from ( sele...