Skip to main content

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:

    <code>SELECT columns
    FROM tables
    WHERE EXISTS ( subquery );</code>

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:

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

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.

Example #2 - NOT EXISTS

The EXISTS condition can also be combined with the NOT operator.

For example,

   <code> SELECT *
    FROM suppliers
    WHERE not exists (select * from orders Where suppliers.supplier_id = orders.supplier_id);</code>

This will return all records from the suppliers table where there are no records in the orders table for the given supplier_id.

Example #3 - DELETE Statement

The following is an example of a delete statement that utilizes the EXISTS condition:

   <code> DELETE FROM suppliers
    WHERE EXISTS
      (select *
        from orders
        where suppliers.supplier_id = orders.supplier_id);</code>


Example #4 - UPDATE Statement

The following is an example of an update statement that utilizes the EXISTS condition:

  <code>  UPDATE suppliers    
    SET supplier_name =     ( SELECT customers.name
    FROM customers
    WHERE customers.customer_id = suppliers.supplier_id)
    WHERE EXISTS
      ( SELECT customers.name
        FROM customers
        WHERE customers.customer_id = suppliers.supplier_id);</code>


Example #5 - INSERT Statement

The following is an example of an insert statement that utilizes the EXISTS condition:

   <code> INSERT INTO suppliers
    (supplier_id, supplier_name)
    SELECT account_no, name
    FROM suppliers
    WHERE exists (select * from orders Where suppliers.supplier_id = orders.supplier_id);
</code>

Comments

Popular posts from this blog

Download SCERTUP BTC 2011 Application Form

Note : Professional Courses Student can also now apply for UP BTC 2011 and date extended. The SCERT of UP was established at Lucknow in 1981 as an apex government organization at the state level. The State Council of Educational  Research  &  Training , U.P., is providing academic support to the District Primary Education Program intended primarily to accelerate the processes of pedagogical renewal in the state.   The Utter Pradesh Education Board Basic Training Certificate or B.T.C Application Form 2011 is now available for download on the Uttar Pradesh State Council of Educational Research & Training (SCERT) official website. Pre-Service Training * Training of Special B.T.C. Trainees. Training Urdu Special B.T.C. in process * Training of Shiksha Mitras, EGS/ AIE Instructors & Literacy functionaries. * Pre-service Training for physical Education and nursery training also being conducted. The thrust areas include  (a) development of curriculum, r

UP TET 2011 Exam Application,UPTET 2011 Result | UPTET 2011 Revised Result

UPTET 2011 Result | UPTET 2011 Revised Result | UPTET 2011 Updated Result |Board of High School and Intermediate Education Uttar Pradesh, Allahabad UP TET Advertisement 2011 and Exam Detail/Admit Card/Call Letter Download UP TET 2011 FORM & DETAILS  GET LATEST DETAIL ABOUT 72825 Primary Teacher Merit List UP TET 2011 Application form submission last date is 18-Oct-2011.Forms will be distributed from PNB bank all over UP.According to latest news Appearing B.Ed student also eligible for TET exam 2011 so they can also submit their forms to concern District.There is no requirement for Rojgar Registration No and Bank receipt within the application form.UP TET 2011 exam results will be avail on http://www.uptet2011.com . Now you can easily get Application from from the P.N.B bank Branches. UPTET 2011 Result | UPTET 2011 Revised Result | UPTET 2011 Updated Result |  Board of High School and Intermediate Education Uttar Pradesh, Allahabad

remove index.php from wordpress on win2003 shared hosting

No need to install any  ISAPI filter to  remove  the  index . php  from  WordPress  permalinks. No need of  .htaccess  file. . Use these simple steps to WordPress Permalinks in IIS 6.0 using Custom 404 Redirect for Windows Shared hosting/manas hosting or any windows shared hosting. When you run wordpress on IIS server your permalinks have to include the prefix /index.php/ which looks ugly and is totally un necessary.If you have these  requirements as follow:  a .IIS for Windows Shared Hosting b .WordPress Site c .Access to change your 404 error page with your web hosting. This can be remove by following simple steps. 1 .  Once installed wordpress blog on your site you’ll want to log into the admin section and select options and then select permalinks. Set your permalinks as you wish. 2 . Then  You’ll create a new text file and name it "404-error.php". In this text file you’re going to include the text: <?php // This is the default file for the s