Posts

Showing posts from March, 2014

Select duplicate records without count or group by

Below is the simple query of selecting duplicate records without using count or group by. /* Create temp variable table */ DECLARE @tempRecords AS TABLE  ( RecordId INT IDENTITY ( 1 , 1 ), FName VARCHAR ( 10 ), LName VARCHAR ( 10 )) /* Insert Data to temp tabel */ INSERT INTO @tempRecords ( FName , LName ) VALUES ( 'A' , 'B' ), ( 'A' , 'B' ), ( 'C' , 'D' ),( 'C' , 'E' ),( 'A' , 'E' ),( 'C' , 'D' ),( 'A' , 'B' ) /* Select All records */ SELECT * FROM @tempRecords /* Select Duplicate records only with all columns */ SELECT DISTINCT a .* FROM @tempRecords a INNER JOIN @tempRecords b ON a . FName = b . FName AND a . LName = b . LName AND a . RecordId <> b . RecordId ORDER BY a . FName , a . LName Here we select duplicate records by self joining.