EXISTS vs JOIN
JOINS are used for combined multiple tables based on a particular condition whereas the EXISTS operator returns only true or false when there is exists any matched rows in the subquery. The EXISTS operator is just used to test the existence in another table where as the JOIN is used for extending the table with the results from another table.
Let’s see an example of INNER JOIN on the two tables based on the match of courseId :
SELECT * FROM CoursesActive
INNER JOIN CoursesInactive
ON CoursesActive.courseId = CoursesInactive.courseId
The result looks like this:
Explanation: The INNER JOIN is done on the tables coursesActive and coursesInactive based on the condition of the match on the courseId, we can see that both tables got combined based on that condition.
SQL Server EXISTS
The EXISTS operator is used when we are dependent on another subquery which can be in the same table or a different table. When the subquery returns any rows the EXISTS operators return true otherwise false. EXISTS Operator simply checks whether the subquery returns any row. It gives true or False based on the existence of rows. In this article, you will get a clear idea about EXISTS Operator in SQL Server.
Syntax:
SELECT * FROM table_name WHERE
column_name EXISTS (subquery)
Explanation: In the above query we fetched all the records if the subquery exists. A SELECT statement that returns rows. The EXISTS
operator is used to check if the subquery returns any rows.
Contact Us