PostgreSQL – AND Operator
The AND
operator in PostgreSQL is a logical operator used to combine multiple conditions in a WHERE
clause or other parts of a query. It’s used to ensure that all the specified conditions are true for a particular row to be included in the query result.
The syntax for using the AND
operator is as follows:
SELECT columns FROM table_name WHERE condition1 AND condition2 AND ... AND conditionN;
Here’s an example of using the AND
operator:
SELECT name FROM employees WHERE department = 'IT' AND salary > 50000;
In this example, the query will retrieve the name of employees who belong to the ‘IT’ department and have a salary greater than $50,000.
You can use the AND
operator to combine any number of conditions to filter rows based on multiple criteria. All conditions joined by AND
must be true for a row to be included in the query result.
Additionally, you can also use parentheses to group conditions and control the order of evaluation when mixing AND
and OR
operators. For example:
SELECT product_name, price FROM products WHERE (category = 'Electronics' OR category = 'Appliances') AND stock > 0;
In this example, the query retrieves the product name and price for products that belong to either the ‘Electronics’ or ‘Appliances’ category and have a positive stock quantity.