SQL Server: EXISTS Condition

The SQL Server (Transact-SQL) EXISTS condition is used in combination with a subquery and is considered to be met if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

  1. SELECT * FROM employees WHERE EXISTS (SELECT * FROM contacts WHERE employees.last_name = contacts.last_name AND employees.first_name = contacts.first_name); 

Example: with Insert Statement

The following is an example of an INSERT that uses the EXISTS condition:

  1. INSERT INTO contacts (contact_id, contact_name) SELECT supplier_id, supplier_name FROM suppliers WHERE EXISTS (SELECT * FROM orders WHERE suppliers.supplier_id = orders.supplier_id); 

The following exp_update

  1. UPDATE suppliers SET supplier_name = (SELECT customers.name FROM customer WHERE customers.customer_id = suppliers.supplier_id) WHERE EXISTS (SELECT customers.name FROM customers WHERE customers.customer_id = suppliers.supplier_id);