1. Find nth salary form Employee:
duplicate records
TO FIND NTH HIGHEST SALARY USING CTE
- SELECT*FROM [DBO].[EMPLOYEE] ORDER BY SALARY DESC
- GO
- WITH RESULT AS
- (
- SELECT SALARY,
- DENSE_RANK() OVER (ORDER BY SALARY DESC) AS DENSERANK
- FROM EMPLOYEE
- )
- SELECT TOP 1 SALARY
- FROM RESULT
- WHERE DENSERANK = 3
duplicate records
SELECT username, email, COUNT(*)
FROM users
GROUP BY username, email
HAVING COUNT(*) > 1
If you want to delete the duplicates, here's a much simpler way to do it than having to find even/odd rows into a triple sub-select:
SELECT id, name, email
FROM users u, users u2
WHERE u.name = u2.name AND u.email = u2.email AND u.id > u2.id
And so to delete:
DELETE FROM users
WHERE id IN (
SELECT id/*, name, email*/
FROM users u, users u2
WHERE u.name = u2.name AND u.email = u2.email AND u.id > u2.id
)
If you want to delete duplicate record with contains one record.
with TEST as(
select ProductId,city, ROW_NUMBER() OVER(PARTITION BY city ORDER BY city) AS T from PRODUCTDETAILS
)
SELECT * FROM TEST WHERE T>1