How to delete duplicate rows
Today I want to give you an example of how delete duplicate rows from a table
Let’s create new table with name students:
CREATE TABLE students (ID NUMBER, NAME VARCHAR2(10), surname VARCHAR2(10))
Now let’s add some duplicate rows to it:
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
And now let’s look to the data of our table:
SELECT * FROM students
1 Nitesh Jauhari
2 Edward Smith
1 Nitesh Jauhari
2 Edward Smith
1 Nitesh Jauhari
2 Edward Smith
Now let’s delete duplicate rows using this script:
DELETE FROM students
WHERE ROWID NOT IN (SELECT MAX (ROWID) FROM students GROUP BY ID)
And now let’s look to students table
SELECT * FROM students
1 Nitesh Jauhari
2 Edward Smith
Thanks.
NJ
Today I want to give you an example of how delete duplicate rows from a table
Let’s create new table with name students:
CREATE TABLE students (ID NUMBER, NAME VARCHAR2(10), surname VARCHAR2(10))
Now let’s add some duplicate rows to it:
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
INSERT INTO students VALUES (1, ‘Nitesh’, ‘Jauhari’);
INSERT INTO students VALUES (2, ‘Edward’, ‘Smith’);
And now let’s look to the data of our table:
SELECT * FROM students
1 Nitesh Jauhari
2 Edward Smith
1 Nitesh Jauhari
2 Edward Smith
1 Nitesh Jauhari
2 Edward Smith
Now let’s delete duplicate rows using this script:
DELETE FROM students
WHERE ROWID NOT IN (SELECT MAX (ROWID) FROM students GROUP BY ID)
And now let’s look to students table
SELECT * FROM students
1 Nitesh Jauhari
2 Edward Smith
Thanks.
NJ
No comments:
Post a Comment