Tuesday, May 10, 2011

Useful Query for SQL Server

Sql server where not in multiple columns
SELECT *
FROM Table1
WHERE not exists (SELECT 1 FROM Table2 where Table1.ClientNumber=Table2.ClientNumber and Table1.CaseNumber=Table2.CaseNumber)

Second highest salary
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary

Find Duplicate record from Table
SELECT col1, col2, count(*)
FROM t1
GROUP BY col1, col2
HAVING count(*) > 1

No comments:

Post a Comment