Your code is flawed. You should never delete posts by issuing SQL directly to the DB. Use WP_Query to find the posts that are duplicates and wp_delete_post to delete them.
Your current method might generate a lot of garbage in the DB.
Side Note: Best way to prevent duplication is not to allow them to be created in the first place.