Suppose you want to find the customer who has placed at least one sales order, you can use the EXISTS operator as follows:
SELECT customerNumber,
customerName
FROM customers
WHERE EXISTS
(SELECT 1
FROM orders
WHERE orders.customernumber = customers.customernumber);
For each row in the customers table, the query checks the customerNumber in the orders table.
If the customerNumber, which appears in the customers table, exists in the orders table, the subquery returns the first matching row. As the result, the EXISTS operator returns true and stops scanning the orders table. Otherwise, the subquery returns no row and the EXISTS operator returns false.
To get the customer who has not placed any sales orders, you use the NOT EXISTS operator as the following statement:
SELECT customerNumber,
customerName
FROM customers
WHERE NOT EXISTS
(SELECT 1
FROM orders
WHERE orders.customernumber = customers.customernumber);