PostgreSQL AND Operator


PostgreSQL AND Operator

The PostgreSQL AND operator is used to combine multiple conditions in a query. This operator is essential for filtering query results where all specified conditions must be true.


Syntax

SELECT columns
FROM table_name
WHERE condition1 AND condition2;

The AND operator has the following components:

  • columns: The columns to be retrieved from the table.
  • table_name: The table from which to retrieve the data.
  • condition1: The first condition to be met.
  • condition2: The second condition to be met.

Example PostgreSQL AND Queries

Let's look at some examples of PostgreSQL AND operator queries:

1. Basic AND Example

SELECT customer_id, customer_name
FROM customers
WHERE customer_id > 1 AND customer_id < 5;

This query retrieves the customer_id and customer_name from the customers table where the customer_id is greater than 1 and less than 5.

2. AND with Multiple Conditions

SELECT product_id, product_name
FROM products
WHERE price > 30.00 AND price < 90.00 AND in_stock = true;

This query retrieves the product_id and product_name from the products table where the price is greater than 30.00, less than 90.00, and the product is in stock.

3. AND with Date Comparison

SELECT order_id, order_date
FROM orders
WHERE order_date > '2024-01-01' AND order_date < '2024-12-31';

This query retrieves the order_id and order_date from the orders table where the order_date is between '2024-01-01' and '2024-12-31'.

4. AND with String Comparison

SELECT customer_id, customer_name
FROM customers
WHERE customer_name LIKE 'J%' AND customer_name <> 'John Doe';

This query retrieves the customer_id and customer_name from the customers table where the customer_name starts with 'J' and is not 'John Doe'.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the AND operator to filter data.

Step 1: Creating a Table

This step involves creating a new table named products to store product data.

CREATE TABLE products (
    product_id SERIAL PRIMARY KEY,
    product_name VARCHAR(100),
    price NUMERIC(10, 2),
    in_stock BOOLEAN
);

In this example, we create a table named products with columns for product_id, product_name, price, and in_stock.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the products table.

INSERT INTO products (product_name, price, in_stock)
VALUES ('Product A', 30.00, true),
       ('Product B', 60.00, false),
       ('Product C', 90.00, true);

Here, we insert data into the products table.

Step 3: Using the AND Operator

This step involves using the AND operator to filter data from the products table.

-- Basic AND
SELECT product_id, product_name
FROM products
WHERE price > 30.00 AND price < 90.00;

-- AND with Multiple Conditions
SELECT product_id, product_name
FROM products
WHERE price > 30.00 AND price < 90.00 AND in_stock = true;

-- AND with Date Comparison
SELECT order_id, order_date
FROM orders
WHERE order_date > '2024-01-01' AND order_date < '2024-12-31';

-- AND with String Comparison
SELECT customer_id, customer_name
FROM customers
WHERE customer_name LIKE 'J%' AND customer_name <> 'John Doe';

These queries demonstrate how to use the AND operator to filter data from the products table, including basic conjunctions, multiple conditions, date comparisons, and string comparisons.

Conclusion

The PostgreSQL AND operator is a fundamental tool for combining multiple conditions and filtering query results where all specified conditions must be true. Understanding how to use the AND operator and its syntax is essential for effective data retrieval and manipulation in PostgreSQL databases.