SQL AND Operator
The AND
operator is used to combine multiple conditions in a SQL WHERE
clause. It ensures that all the specified conditions must be true for the rows to be included in the result set.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example 1: Using AND with Two Conditions
SELECT *
FROM employees
WHERE department = 'Sales' AND salary > 50000;
This query selects all columns from the “employees” table where the “department” is ‘Sales’ and the “salary” is greater than 50,000.
Example 2: Using AND with Multiple Conditions
SELECT *
FROM customers
WHERE country = 'USA' AND city = 'New York' AND age > 30;
This query selects all columns from the “customers” table where the “country” is ‘USA’, the “city” is ‘New York’, and the “age” is greater than 30.
Example 3: Using AND with Different Data Types
SELECT *
FROM products
WHERE category = 'Electronics' AND in_stock = true AND price <= 300;
This query selects all columns from the “products” table where the “category” is ‘Electronics’, the “in_stock” column is true, and the “price” is less than or equal to 300.
The AND
operator is a powerful tool for filtering data based on multiple conditions. It allows for precise control over the query results, ensuring that only the rows meeting all specified criteria are included.