-1

I am refreshing myself on SQL and that database I was working on glitched (online training database, not important) and duplicated 1 of my entries. It's just a firstname, lastname, age database.

I want to remove the duplicate entry - but can't just do a simple remove/where statement because it would delete both entries.

I know how to remove all the duplicates from a database using a temporary table and renaming it - but that seems like overkill and I suspect there is an easier way.

Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50

3 Answers3

0

Which RDBMS?

In most cases, you can do something along the lines of:

set rowcount 1
delete from tbl where firstname = 'john' and lastname = 'doe'

There are more complex options for pruning duplicates, but this should work for you.

breno
  • 16
  • 1
0

In SQL Server you could do it like this:

WITH MyCTE AS
(
    SELECT firstname, 
           lastname, 
           age, ROW_NUMBER() OVER (ORDER BY firstname, lastname, age) AS rn
    FROM TableName
)
DELETE FROM MyCTE WHERE rn > 1
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
0

If the database is an Oracle database you can use: rowid.

See the following link: Delete duplicate rows from Oracle tables.

For SQL server you can use the rowid also. See this thread on stackoverflow.

if the database is MySQL there is another thread on stackoverflow.

Community
  • 1
  • 1