PostgreSQL IN Operator
IN
TheINoperator allows you to specify a list of possible values in the WHERE clause.
TheINoperator is a shorthand for multipleOR conditions.
Example
Return all customers from 'Germany', France' or 'UK':
WHERE country IN ('Germany', 'France', 'UK');
NOT IN
By using theNOT keyword in front of theINoperator, you return all records that are NOT any of the values in the list.
Example
Return all customers that are NOT from 'Germany', France' or 'UK':
WHERE country NOT IN ('Germany', 'France', 'UK');
IN (SELECT)
You can also use aSELECT statement inside the parenthesis to return all recordsthat are in the result of theSELECT statement.
Example
Return all customers that have an order in theorders table:
WHERE customer_id IN (SELECT customer_id FROM orders);
NOT IN (SELECT)
The result in the example above returned 89 records, that means that there are 2 customers that haven't placed any orders.
Let us check if that is correct, by using theNOT IN operator.
Example
Return all customers that have NOT placed any orders in theorders table:
WHERE customer_id NOT IN (SELECT customer_id FROM orders);

