Tuesday, October 27, 2015

Top 10 commonly asked Sql server queries in interview

In this article I am going discuss the top 10 commonly asked Sql server queries in interview

1  1.     Find the Highest Salary of Employee
Select MAX(salary) from dbo.Tb_Employee

2    2.    Find the nth number highest salary
In this blow given I have find the second highest salary.
Select Top 1 Salary from(Select distinct top 2 salary from dbo.Tb_Employee order by salary desc) a order by salary asc

3  3.     Find the nth number lowest salary
Using below given query I am finding the second lowest salary.
Select Top 1 Salary from(Select distinct top 2 salary from dbo.Tb_Employee order by salary asc) a order by salary desc

4     4.      Find the duplicate record
Here I am finding the duplicate record (name) exist in table.
select name, count(*)  as duplicate from dbo.Tb_Employee group by name



5   5.      Calculate the Running Total
Select name,phone,ems.salary,(select sum(salary) from dbo.Tb_Employee where id <=ems.id) as Running from dbo.Tb_Employee ems 

6   6.     Convert the date
select CONVERT(varchar(20),getdate(),103)

For more detail read this article date format in sql

7   7.      Create a clone of existing table without data
Select * into Tb_Clone from dbo.Tb_Employee where 1=2

8  8.    Generate row number without rownum
Select *,(Select count(*) from Tb_Employee i where o.name >= i.name) row_num from Tb_Employee o order by row_num

9   9.     Find the unique values without using distinct
select salary from Tb_Employee group by salary

1  10.  Swap the values of two column
update dbo.Tb_Employee set salary= Phone, Phone=salary


  I hope you enjoyed this article. 

1 comment: