In SQL, the EXISTS operator is useful to show the results if the subquery returns data. Generally, we will use EXISTS
operator in the WHERE
clause to check whether the subquery has return values or not.
Following is the syntax of exists operator in the SQL server.
If you observe the above SQL EXISTS operator syntax, we will get values only when the subquery returns any data or rows otherwise, it will not return any data.
Now, we will check exists operator with examples for that create “EmployeeDetails” table by using the following script in your SQL database.
Once we execute the above script, our table “EmployeeDetails” will create and return the result as shown below.
Now run the following examples to check EXISTS operator in the SQL server.
Following SQL exists operator example will return employee details whose values match with the values returned by the subquery.
When we execute the above SQL exists operator query, we will get the following result.
Following SQL exists operator query will return employee details whose values match with the values returned by the subquery.
When we execute the above SQL exists operator query, we will get the following result.
The EXISTS operator will return all the records whose values match with the subquery return values. Suppose, if we use NOT
with EXISTS
operator, it will return all the records whose values do not match the subquery values.
Following SQL not exists operator example will return records whose values do not exist in the subquery returned result or rows.
When we execute the above SQL not exists operator query, we will get the following result.
Following SQL not exists operator example will return records whose values do not exist in the subquery returned result or rows.
When we execute the above SQL not exists operator query, we will get the following result.