Home » SQL & PL/SQL » SQL & PL/SQL » How to delete records found with MINUS
icon5.gif  How to delete records found with MINUS [message #484566] Wed, 01 December 2010 11:04 Go to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
I wish to delete records found with function MINUS from file1, which is a result of records not found in file2.

Both codes are taken way too long. Any Help is appreciated

ps. none of the columms are unique, and some records have no value in some columns

Thanks Danny

DELETE
FROM file1 y
WHERE EXIST (SELECT *
FROM (
SELECT a.col1, a.col2, a.col3, a.col4
FROM file1 a
MINUS
SELECT b.col1, b.col2, b.col3, b.col4
FROM file2 b
) z
WHERE z.col1||z.col2||z.col3||z.col4 =
y.col1||y.col2||y.col3||y.col4
);



DELETE
FROM file1 y
WHERE EXIST (SELECT *
FROM file2 z
WHERE z.col1||z.col2||z.col3||z.col4 =
y.col1||y.col2||y.col3||y.col4
);
Re: How to delete records found with MINUS [message #484570 is a reply to message #484566] Wed, 01 December 2010 11:42 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
why are you doing this:
WHERE z.col1||z.col2||z.col3||z.col4 = 
y.col1||y.col2||y.col3||y.col4
Re: How to delete records found with MINUS [message #484572 is a reply to message #484570] Wed, 01 December 2010 11:58 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
file1 has one column that file2 does not have.
I had to combine the columns as only the combination makes it unique.
Re: How to delete records found with MINUS [message #484573 is a reply to message #484572] Wed, 01 December 2010 12:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I had to combine the columns as only the combination makes it unique.
No you do not need to combine

WHERE z.col1 = y.col1
and z.col2 = y.col2
and z.col3 = y.col3
and z.col4 = y.col4

Are all columns involved VARCHAR2 datatypes?
Re: How to delete records found with MINUS [message #484574 is a reply to message #484573] Wed, 01 December 2010 12:05 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
i tried so but it didn work out since not all columns have values.
At least I think that was what caused the error in the query.

oh and yes some columns have nummeric values

[Updated on: Wed, 01 December 2010 12:08]

Report message to a moderator

Re: How to delete records found with MINUS [message #484575 is a reply to message #484574] Wed, 01 December 2010 12:13 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please realize that we don't have your tables & we don't have your data.
Therefore we can't run, test or improve your posted SQL.
It would be helpful if you provided DDL (CREATE TABLE ...) for tables involved.
It would be helpful if you provided DML (INSERT INTO ...) for test data.
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
It would be helpful if you provided expected/desired results & a detailed explanation how & why the test data gets transformed or organized.
Re: How to delete records found with MINUS [message #484583 is a reply to message #484575] Wed, 01 December 2010 13:21 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
I''ll post it in the morning. Im at home right now...
Re: How to delete records found with MINUS [message #484624 is a reply to message #484566] Thu, 02 December 2010 02:05 Go to previous messageGo to next message
msmallya77
Messages: 28
Registered: June 2009
Location: Ahmedabad
Junior Member
Hi,

Quote:
I wish to delete records found with function MINUS from file1, which is a result of records not found in file2


converting above is,

confirm your result set before deleting records as

select * from file1 a
where not exists (select * from file2 b where a.key=b.key)


Then delete records as

delete * from file1 a
where not exists (select * from file2 b where a.key=b.key)

Regards,

MSMallya
Re: How to delete records found with MINUS [message #484625 is a reply to message #484624] Thu, 02 December 2010 02:42 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
How does this answer the question? Where is MINUS in your statements?

Regards
Michel

[Updated on: Thu, 02 December 2010 02:42]

Report message to a moderator

Re: How to delete records found with MINUS [message #484659 is a reply to message #484625] Thu, 02 December 2010 04:40 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
I checked the query again.
with the minimal colums that makes the row unique, it runs quick.

DELETE
FROM file1 y
WHERE EXIST (SELECT *
FROM (
SELECT a.col1, a.col2, a.col3, a.col4
FROM file1 a
MINUS
SELECT b.col1, b.col2, b.col3, b.col4
FROM file2 b
) z
WHERE z.col1 = y.col1 AND
z.col2 = y.col2 AND
z.col3 = y.col3

);

but adding the check on col4 wich sometime has no value, it deletes no record!

Doing the same run but with the concatenate function || it dramatically runs longer.


Re: How to delete records found with MINUS [message #484662 is a reply to message #484625] Thu, 02 December 2010 04:42 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
Michel Cadot wrote on Thu, 02 December 2010 09:42
How does this answer the question? Where is MINUS in your statements?

Regards
Michel



It would be be nicer if the MINUS function is able to returns the rownumbers as well.

That way I don't have to go trough the tabel again.

I used the concatenate function to compare nearly all columns in both tables. Now I have to make sure that the combination of specified columns is unique. Otherwise i would be deleting the wrong records.

[Updated on: Thu, 02 December 2010 04:50]

Report message to a moderator

Re: How to delete records found with MINUS [message #484671 is a reply to message #484662] Thu, 02 December 2010 04:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
We are still waiting for what Blackswan asked and for your Oracle version.

Regards
Michel
Re: How to delete records found with MINUS [message #484673 is a reply to message #484662] Thu, 02 December 2010 04:53 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
DELETE
FROM file1 y
WHERE EXIST (SELECT *
             FROM (SELECT a.col1, a.col2, a.col3, a.col4
                   FROM file1 a
                   MINUS
                   SELECT b.col1, b.col2, b.col3, b.col4
                   FROM file2 b
                  ) z
             WHERE z.col1 = y.col1 
             AND z.col2 = y.col2 
             AND z.col3 = y.col3 
             AND (z.col4 = y.col4
                  OR (z.col4 IS NULL AND y.col4 IS NULL ))

Re: How to delete records found with MINUS [message #484675 is a reply to message #484673] Thu, 02 December 2010 04:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
DELETE
FROM file1 
WHERE rowid in 
       (SELECT rowid
        FROM file1 a
        WHERE (a.col1, a.col2, a.col3, a.col4) IN
              (SELECT a.col1, a.col2, a.col3, a.col4
               FROM file1 a
               MINUS
               SELECT b.col1, b.col2, b.col3, b.col4
               FROM file2 b
              )
/

Regards
Michel
Re: How to delete records found with MINUS [message #484677 is a reply to message #484671] Thu, 02 December 2010 05:02 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
Michel Cadot wrote on Thu, 02 December 2010 11:51
We are still waiting for what Blackswan asked and for your Oracle version.

Regards
Michel


It's Oracle 10g.
It's taking about 15-20 minutes to go through 2 x 43000 records of the tabels involved.

I have another tabel with 1500 records which is processed the same way. Ofcourse this is taking less time.

Re: How to delete records found with MINUS [message #484678 is a reply to message #484677] Thu, 02 December 2010 05:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
10g is NOT a version, it is a marketing name.
10.2.0.4.0 is a version number.

You forgot to read:
Quote:
Please realize that we don't have your tables & we don't have your data.
Therefore we can't run, test or improve your posted SQL.
It would be helpful if you provided DDL (CREATE TABLE ...) for tables involved.
It would be helpful if you provided DML (INSERT INTO ...) for test data.


So is your problem, "how to express the DELETE statement with MINUS?" or "how to get delete faster?"?
If you don't post your actual question, you will not have the answer for it.

Regards
Michel

[Updated on: Thu, 02 December 2010 05:09]

Report message to a moderator

Re: How to delete records found with MINUS [message #484679 is a reply to message #484673] Thu, 02 December 2010 05:10 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
cookiemonster wrote on Thu, 02 December 2010 11:53
DELETE
FROM file1 y
WHERE EXIST (SELECT *
             FROM (SELECT a.col1, a.col2, a.col3, a.col4
                   FROM file1 a
                   MINUS
                   SELECT b.col1, b.col2, b.col3, b.col4
                   FROM file2 b
                  ) z
             WHERE z.col1 = y.col1 
             AND z.col2 = y.col2 
             AND z.col3 = y.col3 
             AND (z.col4 = y.col4
                  OR (z.col4 IS NULL AND y.col4 IS NULL ))



Error on this query
ORA-00920: invalid relational operator
Re: How to delete records found with MINUS [message #484681 is a reply to message #484679] Thu, 02 December 2010 05:15 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Missing bracket at the end.
Re: How to delete records found with MINUS [message #484683 is a reply to message #484678] Thu, 02 December 2010 05:18 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
Michel Cadot wrote on Thu, 02 December 2010 12:08
10g is NOT a version, it is a marketing name.
10.2.0.4.0 is a version number.

You forgot to read:
Quote:
Please realize that we don't have your tables & we don't have your data.
Therefore we can't run, test or improve your posted SQL.
It would be helpful if you provided DDL (CREATE TABLE ...) for tables involved.
It would be helpful if you provided DML (INSERT INTO ...) for test data.


So is your problem, "how to express the DELETE statement with MINUS?" or "how to get delete faster?"?
If you don't post your actual question, you will not have the answer for it.

Regards
Michel


it's 10.2.0.4.0 64 bit

If it's possible to express the delete statement with MINUS then I would like to know the syntax.
If not then deleting it faster would be my next question.

But I'mm allready happy with just checking the colomns as the time as reduced to a few seconds.

Re: How to delete records found with MINUS [message #484687 is a reply to message #484681] Thu, 02 December 2010 05:22 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
cookiemonster wrote on Thu, 02 December 2010 12:15
Missing bracket at the end.


that what a typo in toad it returns that error.
Re: How to delete records found with MINUS [message #484689 is a reply to message #484687] Thu, 02 December 2010 05:24 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Don't know what you mean?
Re: How to delete records found with MINUS [message #484690 is a reply to message #484687] Thu, 02 December 2010 05:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
kingman wrote on Thu, 02 December 2010 12:22
cookiemonster wrote on Thu, 02 December 2010 12:15
Missing bracket at the end.


that what a typo in toad it returns that error.

If you posted what we asked this will not happen as we could test our statement before post them.

Quote:
If it's possible to express the delete statement with MINUS then I would like to know the syntax.


You have several examples there.

Regards
Michel

Re: How to delete records found with MINUS [message #484694 is a reply to message #484689] Thu, 02 December 2010 05:38 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
sorry about that.

i tried to tell you that is was a type error, missed the bracket in my message but not in TOAD. (That's where i fire my queries)
Re: How to delete records found with MINUS [message #484696 is a reply to message #484690] Thu, 02 December 2010 05:41 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member

Quote:
If it's possible to express the delete statement with MINUS then I would like to know the syntax.


You have several examples there.

Regards
Michel

[/quote]


I'll try your in a couple of minutes. First I have to finish my lunch.

The problem is that I'm using internet via a citrix farm (data is not accessible from this citrix server) and company regulation does not allow code or data to be send out.

Re: How to delete records found with MINUS [message #484699 is a reply to message #484694] Thu, 02 December 2010 05:51 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
kingman wrote on Thu, 02 December 2010 11:38
sorry about that.

i tried to tell you that is was a type error, missed the bracket in my message but not in TOAD. (That's where i fire my queries)


The query I originally posted is missing a bracket. So if you typo'd it, it's presumably missing two. Take mine, add a ) at the end.

If that doesn't work, run the query in sqlplus (if you've got toad you've got sqlplus) and copy and paste the full execution here. SQLplus is a lot better at highlighting the source of errors than TOAD is.
Re: How to delete records found with MINUS [message #484700 is a reply to message #484696] Thu, 02 December 2010 05:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Do you really have 2 tables named file1 and file2 with column col1, col2... which your entreprise sees so confidential that you can't post a statement that allows to create them?

Regards
Michel
Re: How to delete records found with MINUS [message #484702 is a reply to message #484696] Thu, 02 December 2010 05:52 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
kingman wrote on Thu, 02 December 2010 11:41

The problem is that I'm using internet via a citrix farm (data is not accessible from this citrix server) and company regulation does not allow code or data to be send out.


So post dummy tables and dummy data. We probably only need a small handful of lines to recreate your exact scenario.
Re: How to delete records found with MINUS [message #484706 is a reply to message #484702] Thu, 02 December 2010 06:08 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
cookiemonster wrote on Thu, 02 December 2010 12:52
kingman wrote on Thu, 02 December 2010 11:41

The problem is that I'm using internet via a citrix farm (data is not accessible from this citrix server) and company regulation does not allow code or data to be send out.


So post dummy tables and dummy data. We probably only need a small handful of lines to recreate your exact scenario.



I was going to, but the query now runs less than 1 second. (That's without col4)
Re: How to delete records found with MINUS [message #484719 is a reply to message #484706] Thu, 02 December 2010 07:09 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member

table static_test
col1 VARCHAR2(64 BYTE),
col2 VARCHAR2(32 BYTE),
col3 VARCHAR2(64 BYTE),
col4 VARCHAR2(128 BYTE),
col5 VARCHAR2(64 BYTE),
col6 DATE DEFAULT NULL,
col7 DATE

table static_test
COL1;COL2;COL3;COL4;COL5;COL6;COL7
serverx;domaina;AMS;incl;;2-12-2010 13:47:13;2-12-2010 13:47:18;
serverx;domaina;NY;excl;;2-12-2010 13:47:13;2-12-2010 13:47:18;
serverz;domainb;HK;incl;xtra1, xtra2;2-12-2010 13:47:13;2-12-2010 13:47:18;
servery;domaina;BXL;incl;;2-12-2010 13:47:13;2-12-2010 13:49:01;


table auto_test
col1 VARCHAR2(64 BYTE),
col2 VARCHAR2(32 BYTE),
col3 VARCHAR2(64 BYTE),
col4 VARCHAR2(128 BYTE),
col5 VARCHAR2(64 BYTE),
col6 DATE DEFAULT SYSDATE

table auto_test
COL1;COL2;COL3;COL4;COL5;COL6
serverx;domaina;AMS;incl;;2-12-2010 13:47:13;
serverx;domaina;NY;excl;;2-12-2010 13:47:13;
serverz;domainb;HK;incl;xtra1, xtra2;2-12-2010 13:47:13;

select *
FROM STATIC_test Y
WHERE EXISTS
(SELECT *
FROM (
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from static_test b
minus
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from auto_test a
) Z
WHERE Z.col1 = y.col1 AND
z.col2 = Y.col2 AND
z.col3 = Y.col3 AND
Z.col4 = y.col4 AND
Z.col6 = y.col6
-- (z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
);
Re: How to delete records found with MINUS [message #484720 is a reply to message #484675] Thu, 02 December 2010 07:15 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
Michel Cadot wrote on Thu, 02 December 2010 11:57
DELETE
FROM file1 
WHERE rowid in 
       (SELECT rowid
        FROM file1 a
        WHERE (a.col1, a.col2, a.col3, a.col4) IN
              (SELECT a.col1, a.col2, a.col3, a.col4
               FROM file1 a
               MINUS
               SELECT b.col1, b.col2, b.col3, b.col4
               FROM file2 b
              )
/

Regards
Michel


No record found with query

select *
FROM static_test
WHERE rowid in
(SELECT rowid
FROM static_test a
WHERE (a.col1, a.col2, a.col3, a.col4, a.col5, a.col6) IN
(
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from static_test a
minus
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from auto_test b
)

);
Re: How to delete records found with MINUS [message #484723 is a reply to message #484720] Thu, 02 December 2010 07:37 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
i commented this line as it's returning an error 'invalid relational operator' (SQL Plus aswell0

-- (z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))

[Updated on: Thu, 02 December 2010 07:38]

Report message to a moderator

Re: How to delete records found with MINUS [message #484724 is a reply to message #484723] Thu, 02 December 2010 07:43 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
-- (z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
     ^ typo    ^2nd typo


Can you please go and read the forum guide, especially the section on how to format your post, and then follow it.
Can you not see how much easier it is to read our queries compared to yours?

EDIT: spotted 2nd typo.

[Updated on: Thu, 02 December 2010 07:44]

Report message to a moderator

Re: How to delete records found with MINUS [message #484729 is a reply to message #484699] Thu, 02 December 2010 08:09 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
kingman wrote on Thu, 02 December 2010 13:37
i commented this line as it's returning an error 'invalid relational operator' (SQL Plus aswell0


I'm very certain that sqlplus gave the same error. I'd be very concerned if it didn't. The reason I said:

cookiemonster wrote on Thu, 02 December 2010 11:51
If that doesn't work, run the query in sqlplus (if you've got toad you've got sqlplus) and copy and paste the full execution here. SQLplus is a lot better at highlighting the source of errors than TOAD is.


Is that if you had we would be looking at something like this:
SQL> SELECT 1 b FROM dual d 
  2  WHERE 'A' IN (SELECT 'A' a FROM dual t WHERE t,a = d.b);
WHERE 'A' IN (SELECT 'A' a FROM dual t WHERE t,a = d.b)
                                              *
ERROR at line 2:
ORA-00920: invalid relational operator
See where the * is? Right below the typo.

We don't ask you do things like that (or to post test cases) to make your life difficult. We ask so that we can get enough information to recreate and/or diagnose your problem so that we can help you fix it.

You appear to be rushing, and it's proving counter-productive. Slow down. Check your queries carefully.
When you've got a nested query that doesn't return the right data break it down. Run the inner select stand alone and see if it's right, then re-add layers until it breaks (I suspect nulls are the culprit).

Also bear in mind that your test case doesn't contain any actual inserts or create tables. You'll generally get better and faster replies if we don't have to do significant editing to recreate your data. Some of us are fitting this in round our day jobs.

Re: How to delete records found with MINUS [message #484736 is a reply to message #484729] Thu, 02 December 2010 08:25 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
First of all I'm very sorry about not getting the info right away..
I'm just starting, and I haven't had a decent course in sql....

I overlooked the extra info. Yes I see the *, it's right under the = sign

(z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
*

I don't know how to get the source to create the tabels and data..

[Updated on: Thu, 02 December 2010 08:25]

Report message to a moderator

Re: How to delete records found with MINUS [message #484740 is a reply to message #484736] Thu, 02 December 2010 08:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.

Post a working Test case: create table and insert statements along with the result you want with these data then we will be able work with your table and data.

Regards
Michel
Re: How to delete records found with MINUS [message #484741 is a reply to message #484740] Thu, 02 December 2010 08:39 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
I found the typo (comma which should have been a dot). Query runs fine.

However the script suddenly runs a lot longer. 1.46 minutes compared to less than one second with extra check on column 5. But ofcourse still a lot better than the 15-20 minutes with concatenate function.

select *
FROM STATIC_test Y
WHERE EXISTS
(SELECT *
FROM (
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from static_test b
minus
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from auto_test a
) Z
WHERE Z.col1 = y.col1 AND
z.col2 = Y.col2 AND
z.col3 = Y.col3 AND
Z.col4 = y.col4 AND
Z.col6 = y.col6 AND
(z.col5 = Y.col5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
);

[Updated on: Thu, 02 December 2010 08:44]

Report message to a moderator

icon14.gif  Re: How to delete records found with MINUS [message #484748 is a reply to message #484741] Thu, 02 December 2010 09:12 Go to previous messageGo to next message
kingman
Messages: 19
Registered: December 2010
Junior Member
Thank you all for your help. I'll go for the check on columns instead of using concatenate.

I'll go and read the forumguide now.

[Updated on: Thu, 02 December 2010 09:15]

Report message to a moderator

Re: How to delete records found with MINUS [message #484836 is a reply to message #484625] Fri, 03 December 2010 01:26 Go to previous messageGo to next message
msmallya77
Messages: 28
Registered: June 2009
Location: Ahmedabad
Junior Member
Hi,

Quote:
How does this answer the question? Where is MINUS in your statements?


Results are same. And Hence no need to use minus qry.
SO, MINUS Query can be avoided.

Here it is

drop table file1;
drop table file2;

create table file1 (col1 char(5), col2 number(3));

insert into file1 values('STATE', 2);
insert into file1 values('STATA', 2);
insert into file1 values('STATS', 2);
insert into file1 values('ESTAT', 2);
insert into file1 values('TASTE', 2);
insert into file1 values('EAAST', 2);
insert into file1 values('SEATS', 2);
insert into file1 values('ESATA', 2);
insert into file1 values('ETATE', 2);

create table file2 (col1 char(5), col2 number(3));

insert into file2 values('GUEST', 2);
insert into file2 values('GATES', 2);
insert into file2 values('STAGE', 2);
insert into file2 values('EAAST', 2);
insert into file2 values('SEATS', 2);
insert into file2 values('ESATA', 2);
insert into file2 values('ETATE', 2);

commit;

select * from file1 a
where not exists (select * from file2 b where a.col1=b.col1)
order by col1;

COL1        COL2
----- ----------
ESTAT          2
STATA          2
STATE          2
STATS          2
TASTE          2

5 rows selected.

delete from file1 a
where not exists (select * from file2 b where a.col1=b.col1);

5 rows deleted.

rollback;

select * from file1 a
where exists (
select * from file1 b where a.col1=b.col1
minus
select * from file2 b where a.col1=b.col1)
order by col1;

COL1        COL2
----- ----------
ESTAT          2
STATA          2
STATE          2
STATS          2
TASTE          2

5 rows selected.

delete from file1 a
where exists (
select * from file1 b where a.col1=b.col1
minus
select * from file2 b where a.col1=b.col1);

5 rows deleted.


Am I correct? Hope So.

Regards,

MSMallya
Re: How to delete records found with MINUS [message #484838 is a reply to message #484836] Fri, 03 December 2010 01:35 Go to previous message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
Results are same. And Hence no need to use minus qry.
SO, MINUS Query can be avoided.

It does not matter the question was: WITH MINUS.

Note that your statements do not handle NULL.

Regards
Michel
Previous Topic: last rank retrieve
Next Topic: Passing an Array
Goto Forum:
  


Current Time: Tue Aug 05 20:05:44 CDT 2025