You have a correlated subquery. This is a bit different from a non-correlated subquery, because it can include references to outer tables.
When using correlated subqueries, always use the table aliases for all table references. This is a good idea in general, but should be followed more attentively for correlated subqueries.
AND SGBSTDN_TERM_CODE_EFF = (SELECT MAX(SGBSTDN.SGBSTDN_TERM_CODE_EFF)
FROM SATURN.SGBSTDN
WHERE SGBSTDN.SGBSTDN_TERM_CODE_EFF <= '200002'
AND SGBSTDN.SGBSTDN_PIDM = SFRSTCR.SFRSTCR_PIDM
)
For each value of SFRSTCR.SFRSTCR_PIDM
(and the other conditions), the subquery is getting the maximum date.
In most versions of SQL, correlated subqueries are allowed in the from
, where
, and having
clauses. (They might also be allowed in order by
.)