Description

The SQL keyword EXISTS is used to test the existence of records in a subquery.

  • It returns TRUE if the subquery returns one or more records.
  • It returns FALSE if the subquery returns no records.

The below SQL fetches the customers having orders of value more than 2000.

SELECT *
FROM customers
WHERE EXISTS (SELECT * FROM orders WHERE orders.customer_id = customers.customer_id AND order_value > 2000);

Similarly, the below SQL fetches the customers having orders of value less than 2000.

SELECT *
FROM customers
WHERE EXISTS (SELECT * FROM orders WHERE orders.customer_id = customers.customer_id AND order_value < 2000);

Similarly, the below SQL fetches the customers having orders of value equal to 2000.

SELECT *
FROM customers
WHERE EXISTS (SELECT * FROM orders WHERE orders.customer_id = customers.customer_id AND order_value = 2000);

Related Links