HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

The SQL EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery.

The EXISTS operator returns TRUE if the subquery returns one or more records.

Syntax

  SELECT column_name(s)
  FROM  table_name 
  WHERE EXISTS (
 SELECT column_name
  FROM  table_name   WHERE condition);
              

Demo Products table


This product table is used in examples:

demo products table

Demo Supplier table


This supplier table is used for examples:

demo suplier table

SQL EXISTS Example


The following SQL statement returns TRUE and lists the suppliers with a product price less than 1000:


Example

 SELECT Supplier_name
 FROM  supplier 
 WHERE EXISTS (
 SELECT Product_name
 FROM  products   WHERE 
products.Supplier_id = supplier.Supplier_id    AND  Price < 1000);
You can click on above box to edit the code and run again.

Output

exists example

The following SQL statement returns TRUE and lists the suppliers with a product price equal to 1100:

Example

 SELECT Supplier_name
 FROM  supplier 
  WHERE EXISTS (
 SELECT Product_name
 FROM  products   WHERE 
products.Supplier_id = supplier.Supplier_id    AND  Price = 1100);
You can click on above box to edit the code and run again.

Output

exists example