Are you asking if the first query will work?
delete from tvsemp e
where rowid>(select min(rowid) from tvsemp m where e.ename=m.ename);
The answer is "yes".
As for how it works, your subquery is a correlated subquery, which basically means it references the outer query. Oracle executes the subquery for each row of tvsemp e
, and the e.ename = m.ename
in the subquery limits the subquery's results to rows where ename
equals the ename
in the outer query's current row.
Getting n - 1
results is exactly what you want: if there are four rows with the name Chay
, you want to delete three of them.
Put another way, if there are four rows with the name Chay
, only one of them will have the minimum ROWID
value. The ROWID
for the other three will be higher than the minimum, so the DELETE
query will delete them.
I hope this makes sense. There's more information about subqueries (including correlated subqueries) here.