Databases Reference
In-Depth Information
When we have a subquery, we can try to optimize
the subquery and the main query separately.
The execution plan for this query and the statistics are shown in the following screenshot:
In step 3, we execute a query—similar to the previous one—changing the way in which
selection is made on the
SALES
table. We use the
EXISTS
operator to verify that for every
SALES
row we are examining, there is at least one related row in the
CUSTOMERS
table with
one of the specified credit limits.
In the subquery we select
NULL
, but we can select every field or scalar value, even
*
(star).
We use
NULL
to explicitly clarify that we are "not selecting" anything from the
CUSTOMERS
table, but we are using the subquery only to filter the outer query on
SALES
.