Friday, 15 February 2013

IMPORTANT SQL QUERIES

1)How to remove duplicate records using cte?
A)

with cte as


select id,name,salary,ROW_NUMBER()
 over(partition by id order by id)
  as DUPS
  from emp40000
 )
 delete from cte where DUPS>1

2)How to find 2nd highest salary?
A)
with cte as
(
select salary,ROW_NUMBER()  over(order by salary asc)
 as highsalaries 
from employee 
)
select salary from cte where highsalaries=2

3)Self join example?

A)select e1.name Employeeselfjoin,e2.Name as manager 
from
Employeeselfjoin e1
inner join
Employeeselfjoin e2
on
e1.EmployeeID=e2.ManagerID






No comments:

Post a Comment