Index: java/testing/org/apache/derbyTesting/functionTests/tests/lang/subqueryFlattening.sql
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/lang/subqueryFlattening.sql	(revision 619058)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/lang/subqueryFlattening.sql	(working copy)
@@ -103,7 +103,8 @@
 values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS();
 
 -- multilevel subqueries
--- flatten all
+-- only flatten bottom of where exists, any, or in with 
+-- exists, any, or in in its own where clause. DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i where exists
         (select * from idx1 ii 
@@ -115,8 +116,9 @@
         (select * from idx1 ii 
          where o.c1 = i.c1 and i.c2 = ii.c1));
 values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS();
--- flatten innermost into exists join, then flatten middle
--- into outer
+-- flatten innermost into exists join, but dont flatten middle into outer as it
+-- is a where exists, any, or in with exists, any, or in in its own where clause. 
+-- DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i 
      where  o.c1 = i.c1 and i.c2 = 1 and exists
Index: java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out	(revision 619058)
+++ java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out	(working copy)
@@ -1176,7 +1176,8 @@
 			qualifiers:
 None
 ij> -- multilevel subqueries
--- flatten all
+-- only flatten bottom of where exists, any, or in with 
+-- exists, any, or in in its own where clause. DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i where exists
         (select * from idx1 ii 
@@ -1191,7 +1192,8 @@
 	null
 Statement Text: 
 	-- multilevel subqueries
--- flatten all
+-- only flatten bottom of where exists, any, or in with 
+-- exists, any, or in in its own where clause. DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i where exists
         (select * from idx1 ii 
@@ -1490,8 +1492,9 @@
 null		stop position: 
 null		qualifiers:
 None
-ij> -- flatten innermost into exists join, then flatten middle
--- into outer
+ij> -- flatten innermost into exists join, but dont flatten middle into outer as it
+-- is a where exists, any, or in with exists, any, or in in its own where clause. 
+-- DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i 
      where  o.c1 = i.c1 and i.c2 = 1 and exists
@@ -1505,8 +1508,9 @@
 Statement Name: 
 	null
 Statement Text: 
-	-- flatten innermost into exists join, then flatten middle
--- into outer
+	-- flatten innermost into exists join, but dont flatten middle into outer as it
+-- is a where exists, any, or in with exists, any, or in in its own where clause. 
+-- DERBY-3301.
 select * from outer1 o where exists
     (select * from idx2 i 
      where  o.c1 = i.c1 and i.c2 = 1 and exists
