I’m new to oracle database, can some help me understand this query. This query eliminates duplicates from table.
DELETE FROM table_name A
WHERE ROWID > (SELECT min(rowid)
FROM table_name B
WHERE A.key_values = B.key_values);
Any suggestions for improving the query are welcome.
Edit: No this is not homework , what I didn’t understand is, what is being done by subquery and what does ROWID > On subquery do ?
This is the Source of the query
Dissecting the actual mechanics:
This is a standard query to delete records from the table named "table_name". Here, it has been aliased as "A" to be referred to in the subquery.
This places a condition on the deletion, such that for each row encountered, the ROWID must meed a condition of being greater than..
This is a subquery that is correlated to the main DELETE statement. It uses the value
A.key_valuesfrom the outside query. So given a record from the DELETE statement, it will run this subquery to find the minimum rowid (internal record id) for all records in the same table (aliased as B now) that bear the samekey_valuesvalue.So, to put it together, say you had these rows
The subquery works out that the min(rowid) for each record based on ALL records with the same
key_valuesis:For the records marked with
**, the conditionbecomes true, and they are deleted.
EDIT – additional info
This answer previously stated that ROWID increased by insertion order. That is very untrue. The truth is that
rowid is just a file.block.slot-on-block - a physical address.http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:53140678334596
Tom’s Followup December 1, 2008 – 6am Central time zone: