Assume you have a table with duplicate rows:
| Table: emp | |
| id (Primary Key) | name | 
| 1 | Ela | 
| 2 | Ramesh | 
| 3 | Ela | 
| 4 | Ashok | 
| 5 | Ela | 
To ensure the ids will match, we need a workaround, which is comparatively simple.
Step 1:
Try to get the ids of the unique items present in the table. This can be done either by MIN or MAX function. For example,SELECT
      MIN(id)
FROM
      emp
GROUP BY
      name;
This will result in the following table:
| min | 
| 1 | 
| 3 | 
| 5 | 
Step 2:
Hah... this gives me the unique id values. This is enough to remove the other rows using a sub query. Here it is:DELETE
FROM
      emp 
WHERE
      id NOT IN
      (
        SELECT
               MIN(id)
        FROM
                emp
        GROUP BY
                name
       );
That's all folks! The result is a cleaned up table without duplicates:
| Table: emp | |
| id (Primary Key) | name | 
| 1 | Ela | 
| 2 | Ramesh | 
| 4 | Ashok | 
Free Blog Counter
