Monday, August 2, 2010

Use of Sql Exists Clause.

SQL: EXISTS Condition

The EXISTS condition is considered "to be met" if the subquery returns at least one row.

The syntax for the EXISTS condition is:

    SELECT columns
    FROM tables
    WHERE EXISTS ( subquery );

The EXISTS condition can be used in any valid SQL statement - select, insert, update, or delete.

Example #1

Let's take a look at a simple example. The following is an SQL statement that uses the EXISTS condition:

    SELECT *
    FROM suppliers
    WHERE EXISTS
      (select *
        from orders
        where suppliers.supplier_id = orders.supplier_id);

This select statement will return all records from the suppliers table where there is at least one record in the orders table with the same supplier_id.

No comments:

Post a Comment