How to combine multiple conditions?

0102

You can combine multiple conditions in a WHERE clause using logical operators such as AND, OR, and NOT. Here’s how each operator works:

  1. AND: Both conditions must be true.
  2. OR: At least one of the conditions must be true.
  3. NOT: Negates a condition.

Example:

SELECT * FROM employees 
WHERE department = 'Sales' AND salary > 50000;

This query retrieves employees who are in the 'Sales' department and have a salary greater than 50,000.

Using OR:

SELECT * FROM employees 
WHERE department = 'Sales' OR department = 'Marketing';

This query retrieves employees who are either in the 'Sales' or 'Marketing' department.

Using NOT:

SELECT * FROM employees 
WHERE NOT department = 'HR';

This query retrieves employees who are not in the 'HR' department.

Combining Conditions:

You can also combine these operators:

SELECT * FROM employees 
WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 50000;

This query retrieves employees who are either in 'Sales' or 'Marketing' and have a salary greater than 50,000.

0 Comments

no data
Be the first to share your comment!