Q12: What is the difference between EXISTS and IN operators?

Difficulty:MediumTopic:Query Semantics
  • IN: Evaluates full inner subquery result lists before matching values against outer query expressions. If the subquery set contains NULL values, NOT IN queries can evaluate to UNKNOWN and return zero rows.
  • EXISTS: Uses semi-join short-circuit logic. It checks for the existence of matching rows and returns TRUE as soon as a single match is found, ignoring subsequent rows.
Key Takeaways
  • EXISTS utilizes short-circuit processing, stopping evaluation immediately upon finding a matching row.
  • Be careful with NOT IN if the target subquery set contains NULL values.