In this post i will show an example of how to select or return only duplicate records in SQL Server.
I have a table named [TBL_DUPLICATEDATA] which has duplicate records. Using Common Table Expression (CTE) , one can select only duplicate records in sql server along with its primary key values (which is id column). It is shown in the image below.
Explaination of the Code
The image above represents two queries , one is Common Table Expression(CTE) query and other represents the query to display the original table
First i wrote CTE to create an intermediate result to find the duplicate records only without displaying primary key values. Then i joined the result of CTE with the original table to get the primary key values as well.