How Compare two columns of Same SQL table to find duplicate records

duplication

I have one table with two columns:

StartDateTime
EndDateTime

What will be best query to find out which rows has same (StartDateTime, EndDateTime)?

Best Answer

You did not specify a particular RDBMS (SQL Server, Oracle, etc.), but here is a quick solution that would probably work on most.

Assuming your table has some kind of unique key (primary key - like ID) to determine the difference between one row and another row, you could use a correlated exists check.

SELECT a.*
FROM table1 a
WHERE EXISTS (
        SELECT *
        FROM table1
        WHERE StartDateTime = a.StartDateTime
            AND EndDateTime = a.EndDateTime
            AND Id <> a.Id
        )