Processing a Query that Contains another Query Section

When you process a Query section that contains another Query section, it forms SQL that uses what are called “derived tables”, which are essentially subquery statements in a FROM clause. The SQL generation phase behaves as follows:

In the previous diagram, the filter on City is set to ‘Los Angeles’. The SQL generated when the above query is processed, per the minimum requirements, would be:

SELECT AL2.STATE, SUM(AL1.UNITS) FROM PCW_SALES AL1,
(SELECT AL3.STORE_ID, AL3.CITY, AL3.STATE, UCASE(AL3.BUYER)
FROM PCW_CUSTOMERS AL3 
WHERE AL3.STORE_TYPE = ‘Discount’) AL2(STORE_ID, CITY, STATE, COMPUTED)
WHERE AL2.STORE_ID = AL1.STORE_ID AND AL2.CITY = ‘Los Angeles’
GROUP BY AL2.STATE