How to use “and” and “or” together in SQL?

In SQL, you can combine conditions using the AND and OR operators to filter data. The AND operator specifies that multiple conditions must all be met simultaneously, while the OR operator specifies that only one of multiple conditions needs to be met.

For instance, if we have a table named “employees” with employee information and we want to filter out employees with salary over 50000 and working in either “New York” or “California”, we can use the following SQL statement:

FROM employees 
WHERE salary > 50000 
AND (location = 'New York' OR location = 'California');

In the example above, the AND operator is used to combine the conditions salary > 50000 and location = ‘New York’ OR location = ‘California’, requiring both conditions to be met simultaneously. The OR operator is used to combine the conditions location = ‘New York’ and location = ‘California’, only one of which needs to be met.

Leave a Reply 0

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