SQL use exists instead of in

In SQL, there is many way to validate if one information exist in another table (or same).
The most used/viewed call is :

  1. SELECT a FROM tablea WHERE a IN (SELECT b FROM tableb)

The problem with this call the long time of execution in case if our tables contains millions of lines.

Our solution to avoid this problem is to use the exists keyword.

  1. SELECT a FROM tablea WHERE EXISTS (SELECT b FROM tableb WHERE tablea.a = tableb.b)

Exists can help you to get result faster.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.