There are a couple of queries I would be looking at; neither one of which will use the
UNION
operator.
The first one is going to be give you exactly what you are asking for by utilizing a CTE (Common Table Expression) being populated via a
SELECT DISTINCT
query
DECLARE @Table TABLE ( ID INT, NAM VARCHAR(8), WAGE INT )
INSERT @Table VALUES (1, 'PAUL', 30), (1, 'PAUL', 30), (2, 'SARA', 40), (2, 'SARA', 40), (3, 'ISLA', 50), (1, 'PAUL', 30)
; WITH Dupes AS ( SELECT Distinct Id, Nam, Wage FROM @Table )
SELECT DuplicateRows =
(SELECT Count(*) FROM @Table)
- (SELECT Count(*) FROM Dupes)
The second query may be of use also; as it will identify which records are being duplicated, and how many duplicates there are. This will be done via the
GROUP BY ... HAVING
clauses
SELECT ID, NAM, WAGE
, Duplicates = Count(*) - 1
FROM @Table
GROUP BY ID, NAM, WAGE
HAVING COUNT(*) > 1