The purpose of the SQL “Exists” and “Not Exists” operator is to check the existence of records in a subquery. One more similar operator is “with” clause that was introduced in 1999 to support the CTE (Common Table Expressions) features. These operators are predefined in the SQL and used together with UPDATE, DELETE, or SELECT statement. The SQL Exists and Not Exists operators must be used together because they are not independent by themselves.
When SQL Exists is used along Where clause, it tests the existence of rows in a subquery.
The subquery word has been used multiple times till the time. Do you actually know the meaning of subquery in the SQL? If not, don’t worry. We will discuss in brief what is a SQL subquery before going to deep further.
A subquery is also named as the inner query or the nested query that is frequently used within other queries. The SQL subquery can be nested with multiple statements like SELECT, INSERT, UPDATE, or DELETE statements etc. Mostly, we use a subquery in SQL with Where and Exists clauses. Here are some basic rules for using subqueries in SQL.
To acquire more knowledge about subquery in SQL, join our self-paced online video classes today at a huge discount.
Learn SQL Server in the Easiest Way
To understand the SQL “Exists” operator in depth, let us discuss an example below. There are two tables where one table stores the Employee details and the second tables stores the employees’ salary paid monthly. Let us see how to use “Exists” operator with these two tables. You can see three sets of dummy data in the table. Here, the first table stores the employee details, second table stores the salary paid to employees, and the third table stores the result for “Exists” query. In the third table, you can see that five records are returned in total and each of them has a unique employee ID that is compared together for both tables.
If there is some ID that does not exist in both tables then the result will be FALSE in that case. For example, “6” ID is given in the first table but not mentioned in the second table, so it is not displayed as the output in the Exists table.
The IN operator can also be used to get the same output. But the question is which operator is more suitable for different situations? If the subquery returns a very large record-set then Exists operator works great here. However, if the record-set is smaller then you must use IN operator in that case.
The “Not Exists” operator works opposite to the Exists operator. It means that no rows are returns when “Not Exists” operator is satisfied. Let us understand the concept with the help of the same example given earlier. The output here will be the opposite. In the Exists table above, these two records were omitted and to retrieve these two rows, you should use “Not Exists” operator. The same output is given by the “Not IN” operator in SQL as it works opposite to the “IN” operator.
Count operator is used to check the total number of rows within a table while Exists operator is used to check the existence of rows when a particular condition is satisfied. In simple words, “Exists” operator may short-circuit after having found the first matching row while “Count” is not allowed to show such type of behavior.
SQL Server Training & Certification
The SQL database works slightly better when using Exists operator instead of Count operator because of a much better cardinality estimate in the middle of an execution plan. Every time you want to check a row for the existence, you should always use Exists operator in that case. Also, the associated costs with Exists operator are almost 30 times less than the Count operator.
“It is obvious checking the existence faster rather than to count all rows together if you are really doing something for the existence only.”
Every SQL operator makes the difference to improve the performance of a query. An SQL operator works with data items and returns the result. Oracle uses operators under different conditions that can be used with queries to filter the result set. In the same Exists operator ensure the best performance when the query contains voluminous data to evaluate.
The reason is Exists operator follows the “at least found” principle in queries. It is set to TRUE if even a single row satisfies the condition. In some cases, it stops the execution at the same place where the result is set to TRUE. However, you should use comparison operators with Exists to continue the table scanning ahead.
With this discussion, we come to the end of the blog. Here, you know the basics of SQL Exists operator and how to use it with a SQL query. We have discussed one detailed example too for your reference to understand the concept in depth. But one example is not enough to practice this operator.
To learn multiple SQL Exists examples, you should join the online SQL certification program right away and start practicing different operators quickly. It will help you to learn SQL practically and get the job in the SQL domain with attractive salary packages.
Register for our Demo class today for more details!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Receive Latest Materials and Offers on SQL Server Course