We are wrapping a POC with a new logo and their DB engineer found the following behavior. They are trying to use a subquery in WHERE clause with a limit to restrict the size of the result set. When the subquery is run standalone and the results are copied into the WHERE clause, it returns rows. When the subquery is copied into the WHERE clause, no results are returned. Some testing reveals that the subquery returns different results depending whether it runs standalone or as a subquery and we'd like to understand why. It would be great if someone suggested a workaround such that the scenarios return the same results as the customer expects, or explain why differing result sets are returned. Here is the use case:
Funny thing is when I tried
(
select uniquePn from intellidata.parameterValues WHERE parameterValueId IN (101, 15)
INTERSECT
select uniquePn from intellidata.parameterValues WHERE parameterValueId = 19
INTERSECT
select uniquePn from intellidata.parameterValues WHERE parameterValueId = 41
) limit 3
It returns
uniquePn
T543V107K006ATS015WAFL
T543D107M016ATS035WAFL
T520D227M004ANE006
SELECT DISTINCT up.uniquePn, up.mfg, up.leafDefId, up.series, up.superAlias, up.obsoleteDate, '' alias, 0 aliasNumber, pv.parameterValueId
FROM intellidata.uniqueParts up
LEFT OUTER JOIN intellidata.parameterValues pv on up.uniquePn = pv.uniquePn
WHERE up.uniquePn IN ('T543V107K006ATS015WAFL', 'T543D107M016ATS035WAFL', 'T520D227M004ANE006')
AND up.mfg IN ( 'KEMET')
AND up.leafDefId IN ( 354, 356, 357, 362, 363, 364, 447)
And above query returns some data. But when I tried
SELECT DISTINCT up.uniquePn, up.mfg, up.leafDefId, up.series, up.superAlias, up.obsoleteDate, '' alias, 0 aliasNumber, pv.parameterValueId
FROM intellidata.uniqueParts up
LEFT OUTER JOIN intellidata.parameterValues pv on up.uniquePn = pv.uniquePn
WHERE up.uniquePn IN ((
select uniquePn from intellidata.parameterValues WHERE parameterValueId IN (101, 15)
INTERSECT
select uniquePn from intellidata.parameterValues WHERE parameterValueId = 19
INTERSECT
select uniquePn from intellidata.parameterValues WHERE parameterValueId = 41
) limit 3)
AND up.mfg IN ( 'KEMET')
AND up.leafDefId IN ( 354, 356, 357, 362, 363, 364, 447)
It returns no results