Skip to content
Advertisement

Is it possible to delete duplicate rows from a table without knowing it’s columns?

Is there a way to delete all the duplicate rows from an arbitrary table in a database when you don’t know the table’s structure (it’s columns) using only plain SQL (without using PL/SQL)?

So, as an example, for the following table:

the result should be as follows:

Advertisement

Answer

Additional answers from the comments:

Establish a CTE (construct this dynamically using parameterized SQL and information schema)

Utilize distinct if you know the tables will have a primary key (as they should)

Lastly, skip the CTE entirely if you want:

All of these statements would be best served constructed dynamically using a stored procedure or the equivalent of exec sp_executeSQL (if avail) in order to grab the column names without knowledge of the columns ahead of time.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement