Explain the difference between EXISTS and IN clauses.

SQL
Medium
106K views

This question compares two methods for checking existence in a subquery. It evaluates your understanding of NULL handling and performance characteristics.

Why Interviewers Ask This

Both clauses achieve similar goals but behave differently with NULLs and perform differently at scale. Interviewers ask this to ensure you choose the most robust and efficient method for your specific scenario.

How to Answer This Question

Explain that EXISTS checks for the presence of at least one row and stops at the first match. Note that NOT IN fails if the subquery contains NULLs. Contrast this with EXISTS/NOT EXISTS which handle NULLs safely and often perform better on large datasets.

Key Points to Cover

  • EXISTS stops at first match
  • NOT IN is sensitive to NULLs
  • EXISTS often performs better on large sets

Sample Answer

EXISTS checks if a subquery returns any rows and stops evaluating as soon as it finds a match, returning a boolean. IN compares a value against a list. A key difference is that NOT IN can return unexpected results if the…

Common Mistakes to Avoid

  • Using NOT IN with potential NULLs
  • Assuming IN is always faster
  • Not understanding short-circuit evaluation

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 101 SQL questions