1

I'm using oracle database and trying to delete duplicate records from that. For the same I've written a below query but getting this error

SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"

Its giving red indicator under outer keyword in Left outer join in query.

Query:

DELETE FROM Duplicate LEFT OUTER JOIN (

   SELECT MIN(RowId) as RWID, STUDENT_NAME, STUDENT_ROLLNO, STUDENT_SUBJECT
   FROM Duplicate 
   GROUP BY STUDENT_NAME, STUDENT_ROLLNO, STUDENT_SUBJECT

) as KeepRows ON
   Duplicate.RowId = KeepRows.RWID
WHERE  KeepRows.RWID IS NULL; 
1
  • 2
    MySQL <> SQL Server <> Oracle; please only tag the RDBMS you are really using. Also, just a post of code doesn't make a question. Put the information we need in your question; along with what you've done to try and solve the problem. Commented Jan 19, 2020 at 12:24

2 Answers 2

2

What I can guess is that you are trying to delete the duplicate records from the Duplicate table. Your syntax doesn't seem correct from Oracle perspective. You may try below query -

DELETE Duplicate D1
 WHERE D1.ROWID NOT IN (SELECT MIN(RowId)
                          FROM Duplicate
                         GROUP BY STUDENT_NAME, STUDENT_ROLLNO, STUDENT_SUBJECT)

Sign up to request clarification or add additional context in comments.

3 Comments

Yes this one of the possible solution of problem, but what's wrong with my query ..?
Any suggestion ..?
Just run the query I posted. This is the correct Oracle supported syntax.
1

Oracle does not permit JOINs in DELETE queries. This is clear if you look at the syntax diagram for DELETE in the documentation.

There are many ways to rewrite this using a subquery in the WHERE clause. The safest is probably the NOT IN method proposed by Ankit because it works even when the columns are NULL and rowid cannot be NULL.

I will point out that for a large number of records, it can be faster to empty the table and reload it:

CREATE TABLE nodups as
    SELECT d.*
    FROM duplicates d
    WHERE D.ROWID NOT IN (SELECT MIN(RowId)
                          FROM Duplicate
                          GROUP BY STUDENT_NAME, STUDENT_ROLLNO, STUDENT_SUBJECT);

TRUNCATE TABLE duplicates;  -- backup first!

INSERT INTO duplicates
    SELECT *
    FROM nodups;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.