25 lines
810 B
Plaintext
25 lines
810 B
Plaintext
If an ``++IN++`` or ``++EXISTS++`` subquery pulls data from the same table(s) involved in the outer SQL statement, there's usually little justification for not simply moving the subquery conditions up into the main statement. Similarly subquery conditions that reference only columns from the outer query should also be "promoted". Doing so will improve readability and likely performance as well, depending on the optimizer.
|
|
|
|
|
|
In very complex situations this may not be possible or might make the SQL statement unnecessarily complicated. But in general, this should be preferred.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
SELECT name
|
|
FROM product
|
|
WHERE id IN (SELECT id FROM product WHERE discount IS NULL) -- Noncompliant
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
SELECT name
|
|
FROM product
|
|
WHERE discount IS NULL
|
|
----
|
|
|
|
|