
188 views
MySQL AND
The MySQL AND
operator is a logical operator used to combine two or more conditions in a SQL query. It is often used in the WHERE
clause of a query to filter rows that meet multiple conditions simultaneously. The AND
operator requires all specified conditions to be true for a row to be included in the result set.
The basic syntax for using the AND
operator is as follows:
SELECT columns
FROM table
WHERE condition1 AND condition2 AND ...;
Here’s how it works:
condition1
,condition2
, and so on are the conditions that you want to combine with theAND
operator.- If all the specified conditions are true for a row, the row is included in the result set.
- If any of the conditions are false for a row, the row is excluded from the result set.
Here are some examples to illustrate the use of the AND
operator:
- Match all records where both the
category
is “Electronics” and theprice
is greater than $500:
SELECT *
FROM products
WHERE category = 'Electronics' AND price > 500;
- Retrieve customers with the name “John” and an email that ends with “@example.com”:
SELECT *
FROM customers
WHERE first_name = 'John' AND email LIKE '%@example.com';
- Find employees who work in the “Sales” department and have a salary greater than $50,000:
SELECT *
FROM employees
WHERE department = 'Sales' AND salary > 50000;
The AND
operator is a crucial tool for building complex queries that involve multiple conditions. It allows you to specify that all conditions must be met for a row to be included in the query result, making it ideal for narrowing down data based on multiple criteria.