; Useful SQL Server Queries
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Useful SQL Server Queries

VIEWS: 249 PAGES: 1

This provides some basic and useful SQL Server queries which are also asked in Interviews.

More Info
  • pg 1
									--Query to find the Highest Salary from the Employee table

Select max(Salary) as 'Highest Salary'
From Employee

--Query to find the Second Maximun Salary from the Employee table

Select max(Salary) as 'Second Maximum Salary'
From Employee
Where Salary not in (Select max(Salary) from Employee)

--Query to find the Third Maximun Salary from the Employee table

Select min(Salary) as 'Third Maximum Salary'
From Employee
Where Salary in (Select distinct top 3 Salary from Employee order by
Salary desc)

--Query to find the least Salary from the Employee table

Select min(Salary) as 'Least Salary'
From Employee

--Selects max salary from each department

select EmployeeID,EmployeeName,Department,Salary
from
( select EmployeeID,EmployeeName,Department,max(Salary) over (partition
by Department) max_sal_dept,Salary from Employee) as part_depid
where salary=max_sal_dept


--Deposit the sum of the deposits of Sunil and Vijay in the account of
Anil

 Update Deposit
  Set Amount=(Select Sum(Amount) From Deposit Where CustomerName in
('Sunil','Vijay'))
  Where CustomerName='Anil'

--Transfer Rs.1000 from the account of Anil to the account of   Sunil

Update Deposit
 Set Amount= Amount-1000 where CustomerName='Anil'
Update Deposit
 Set Amount=Amount + 1000 where CustomerName='Sunil'

--Give 10% interest to all depositors

Update Deposit
 Set Amount=Amount*1.1

								
To top