Question : How to write delete query based on select query output in db2


Hi,

I have the following sql select query.Can you please let me know how to delete the data that would come from the below select query.

select a.*  from table1 a ,table2 b
where a.col1 = b.col1
and   b.col2 = 1
and  a.col2 < b.col2  with ur;

Any help is greatly appreciated.Thank you.

Answer : How to write delete query based on select query output in db2

While the code provided by DaveSlash will work, it is not the most efficient way to code the solution.  If there are a significant amount of rows in table1 or table2, the execution time for the DELETE will be slower compared to using the SQL provided below.  The improved performance can be verified by running EXPLAIN against both queries and looking at the timeron cost associated with each.

The suggested query for best performance is:

delete from table1 a
where exists (
select '1'
from table2 b
where a.col1 = b.col1
and   b.col2 = 1
and  a.col2 < b.col2
);


Random Solutions  
 
programming4us programming4us