In DB2, we often use the predicate EXISTS to verify if a row is present in a table based on our required criteria. We do not wish to get any data from the table being read on the right side of EXISTS, we just want to see if some row is present or not.
In this case, EXISTS should be coded as
SELECT A,B,C FROM TABLE1 WHERE D EXISTS ( SELECT 'X' FROM TABLE2 );
Note: X can also replace a column name from TABLE2
In the existing code, you might see EXISTS used as below, where the complete matching data (by using SELECT *) from TABLE2 is obtained, to check for its existence. We will have performance degradation here. We need to ensure to have the code as the above example, where we get only a constant or single column.
SELECT A,B,C FROM TABLE1 WHERE D EXISTS ( SELECT * FROM TABLE2 );