Let us dive in and see how the AND operator plays in our SQL statements.
SQL AND Operator
As you can guess, the AND operator is a logical operator that allows you to combine two or more Boolean expressions and treat them as one. This means that all the conditions must be true for the condition to be applied.
You will find the AND operator used in a WHERE clause in conjunction with other SQL statements such as SELECT, UPDATE, and DELETE.
This allows you to limit the scope of which the action performs. For example, using a DELETE statement, you can use the WHERE clause and the AND keyword to ensure that only the data that satisfy the specified condition is removed.
The syntax of the AND operator can be expressed as shown below:
You can have as many Boolean expressions as you wish. However, remember that all the specified expressions MUST evaluate to be true. If one expression is false, the entire statement is treated as false.
NOTE: NULL values are also treated as false.
Suppose we have sample data as shown in the queries below:
To find the developers working in the “devops” department and have a salary higher than 110000, we can run a query as shown:
The query should find the matching records and return them as shown:
In this case, there is only one record, as shown above. You can specify more conditions to filter out your data further using the AND keyword.
In this post, we discussed how to use the AND operator in SQL to combine multiple Boolean expressions in your queries.
Thanks for reading. Check our SQL OR operator tutorial to learn more about the OR operator and how it works compared to the AND operator.