I’ve been given a query like this
SELECT * FROM dayTable
LEFT JOIN ProcessTable firstprocess
ON dayTable.id = a.daytablefkid
AND firstprocess.processid in (1234)
AND firstprocess.daytablefkid
IN (SELECT id
FROM daytable
WHERE dayTable.DateFrom > @dateFrom
AND dayTable.DateTo < @dateTo)
LEFT JOIN ProcessTable secondprocess
ON daytable.id = secondprocess.daytablefkid
and secondprocess.processid in (4567)
IN (SELECT id
FROM daytable
WHERE dayTable.DateFrom > @dateFrom
AND dayTable.DateTo < @dateTo)
WHERE dayTable.DateFrom > @dateFrom AND dayTable.DateTo < @dateTo
tHe first thing i noticed is that the first IN-statement “IN(XXXX)” in the joins can be converted to a WHERE-statement, but the second IN statement in both joins is a bit confusing because the same filtering is also done at the end on the query.
What I’m wondering if they are indeed redundant or can the resultset be different if they are removed?
Yes, the
IN (SELECT id FROM daytable WHERE dayTable.DateFrom > @dateFrom AND dayTable.DateTo < @dateTo)-parts are redundant, since you already joined on the daytable, on which you have the sameWHERE-clause.This query should give the same results: