Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
ClausesStatementsKeywordsExpressionsFunctions

MySQL AND Keyword

The `AND` keyword in MySQL is used to combine multiple conditions in SQL statements, ensuring that all specified conditions must be true for the rows to be included in the result set. It is often used in conjunction with `WHERE`, `HAVING`, `ON`, and other clauses to refine data retrieval.

Usage

The `AND` keyword is utilized when there is a need to apply more than one condition to a query. It helps in filtering results based on multiple criteria, ensuring that only rows meeting all conditions are returned.

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

In this syntax, `condition1` and `condition2` are the criteria that must both be true for a row to be selected.

Examples

1. Basic AND Condition

sql
SELECT *
FROM orders
WHERE status = 'Shipped' AND customer_id = 123;

This query selects all columns from the `orders` table where the `status` is 'Shipped' and the `customer_id` is 123.

2. Combining Multiple Conditions

sql
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales' AND salary > 50000 AND hire_date < '2020-01-01';

Here, the query retrieves the first and last names of employees in the Sales department with a salary greater than 50,000 and hired before January 1, 2020.

3. Using AND with JOIN

sql
SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
WHERE orders.total_amount > 100 AND customers.region = 'North';

This query combines data from `orders` and `customers` tables using `JOIN` and filters results where the `total_amount` is greater than 100 and the customer's region is 'North'.

4. Using AND in ON Clause

sql
SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id AND customers.active = 1;

This query utilizes the `AND` keyword directly in the `ON` clause to ensure only active customers are joined.

5. Using AND in HAVING Clause

sql
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 10 AND AVG(salary) > 60000;

In this example, the `HAVING` clause uses `AND` to filter groups where the number of employees is greater than 10 and the average salary exceeds 60,000.

Tips and Best Practices

  • Use parentheses for clarity. When combining `AND` with `OR`, use parentheses to ensure the correct order of evaluation.
  • Optimize conditions. Place the most restrictive condition first to enhance query performance.
  • Be mindful of data types. Ensure conditions are comparable and of compatible data types to avoid unexpected results.
  • Test complex queries. Break down and test complex conditions individually to ensure each part works as expected before combining them.
  • Order of conditions matters. The sequence of conditions can impact both performance and readability, especially in complex queries.
  • Avoid logical errors. Be cautious when mixing `AND` and `OR` without parentheses, as this can lead to unexpected logical errors.