SQL AND Operator

📘 SQL AND Operator

The AND operator in SQL is used in the WHERE clause to combine two or more conditions.
A row is returned only if all conditions are TRUE.


1. Basic Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2;

2. Example: Filter Rows Using Multiple Conditions

Get employees from the IT department AND earning more than 60,000:

SELECT name, department, salary
FROM employees
WHERE department = 'IT'
AND salary > 60000;

✔ Both conditions must be true.


3. Using AND with Multiple Conditions

SELECT *
FROM orders
WHERE status = 'completed'
AND order_date >= '2024-01-01'
AND amount > 100;

4. AND vs OR

  • AND → all conditions must be TRUE

  • OR → at least one condition must be TRUE

Example:

SELECT *
FROM employees
WHERE department = 'HR'
AND full_time = TRUE;

5. AND with Parentheses (Recommended for Complex Logic)

Parentheses help avoid logic mistakes:

SELECT *
FROM products
WHERE category = 'Electronics'
AND (price BETWEEN 100 AND 500)
AND in_stock = TRUE;

6. Using AND with LIKE

SELECT *
FROM customers
WHERE name LIKE 'A%'
AND country = 'USA';

7. Using AND with IN

SELECT *
FROM employees
WHERE department IN ('IT', 'Finance')
AND active = TRUE;

8. Real-World Example

Get active users who signed up after 2023 and verified their email:

SELECT user_id, email
FROM users
WHERE created_at > '2023-01-01'
AND email_verified = TRUE
AND status = 'active';

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *