MySQL

Chapter 7 - DQL (Data Query Language)

'AND' and 'OR' Operators

In MySQL, the AND and OR operators are used in the WHERE clause to filter query results based on multiple conditions. The AND operator ensures that all specified conditions must be true for a row to be included in the result set, while the OR operator allows a row to be included if at least one of the conditions is true. These operators are essential for constructing more complex queries where multiple criteria must be applied.

Here’s a detailed guide on using AND and OR with examples and tips for new students:

  1. Using the AND Operator:

    • The AND operator is used when all conditions in the query must be met for the row to be returned.
    • Syntax:
    SELECT * FROM employees WHERE department_id = 3 AND salary > 50000;
    • This query returns all employees who belong to department 3 and have a salary greater than 50,000. Both conditions must be true for a row to be included.
  2. Using the OR Operator:

    • The OR operator is used when at least one condition must be true for a row to be included in the result set.
    • Example:
    SELECT * FROM employees WHERE department_id = 3 OR salary > 50000;
    • This query returns employees who either belong to department 3 or have a salary greater than 50,000. Rows satisfying either condition will be included.
  3. Combining AND and OR in a Single Query:

    • You can combine AND and OR operators to create more complex conditions. When combining these operators, parentheses should be used to clarify the order of evaluation.
    • Example:
    SELECT * FROM employees WHERE (department_id = 3 OR department_id = 4) AND salary > 50000;
    • This query returns employees who belong to either department 3 or department 4 and have a salary greater than 50,000. The parentheses ensure that the OR condition is evaluated first.
  4. Using AND and OR with NOT:

    • You can also use the NOT operator in combination with AND and OR to exclude specific results.
    • Example:
    SELECT * FROM employees WHERE NOT (department_id = 2) AND salary > 60000;
    • This query returns employees who do not belong to department 2 and have a salary greater than 60,000.
  5. Order of Evaluation Without Parentheses:

    • When combining AND and OR without parentheses, the AND conditions are evaluated before the OR conditions by default.
    • Example:
    SELECT * FROM employees WHERE department_id = 3 OR salary > 50000 AND hire_date > '2020-01-01';
    • In this query, the salary > 50000 AND hire_date > '2020-01-01' condition is evaluated first, so it retrieves employees who either belong to department 3 or have a salary greater than 50,000 and were hired after January 1, 2020.
  6. Using AND and OR in Joins:

    • The AND and OR operators are also commonly used in queries that involve joins between multiple tables.
    • Example:
    SELECT e.employee_id, e.first_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE d.branch_id = 1 AND (e.salary > 50000 OR e.hire_date > '2022-01-01');
    • This query returns employees from branch 1 who either have a salary greater than 50,000 or were hired after January 1, 2022.
  7. Performance Considerations:

    • When combining AND and OR in large datasets, query performance can be affected, especially when columns involved are not indexed. Ensure that commonly used columns in your conditions are indexed for faster query execution.
    • Example:
    SELECT * FROM employees WHERE department_id = 1 AND salary > 70000;
    • If both department_id and salary are indexed, this query will perform more efficiently.

The AND and OR operators are essential for building powerful and flexible queries that can retrieve data based on multiple conditions. Understanding how to combine them correctly and efficiently will help you retrieve more accurate and relevant data from your database.

Tansy SQL Course | 'AND' and 'OR' Operators | Chapter 7 | Lesson 12 - Video Thumbnail

Test code

Choose male customers located in the state of New York.

SELECT * 
FROM org_client 
WHERE gender = 'M' 
AND state = 'NY';
Try it now

Select customers either from the state of Pennsylvania or the city of Miami.

SELECT * 
FROM org_client 
WHERE city = 'Miami' 
OR state = 'PA';
Try it now

Choose customers from the state of New York who, at the secondary level, have a credit limit greater than 7000 or reside in the city of Albany.

SELECT * 
FROM org_client 
WHERE state = 'NY' 
AND (city = 'Albany' OR credit_limit > 7000);
Try it now

SQL's AND and OR operators

Here are examples illustrating the use of SQL's AND and OR operators.

Example 1 - Raw data from client table

i

Example 1 - AND operator

SELECT * FROM org_client WHERE gender = 'M' AND state = 'NY';

Example 1 - Query data mapping

i

In the given image, the green hue indicates the data that has been chosen or meets the criteria specified in our query. When using the AND operator, both column values must satisfy the specified criteria.

Example 1 - Query Output

i

Example 2 - OR operator

SELECT * FROM org_client WHERE city = 'Miami' OR state = 'PA';

Example 2 - Query data mapping

i

In the given image, the green hue indicates the data that has been chosen or meets the criteria specified in our query. In the case of the OR operator, at least one of the column values needs to meet the specified criteria.

Example 2 - Query Output

i

Example 3 - AND operator

SELECT * FROM org_client WHERE state = 'NY' AND (city = 'Albany' OR credit_limit > 7000);

Example 3 - Query data mapping

i

In the given image, the green hue indicates the data that has been chosen or meets the criteria specified in our query. In this instance, a specific row must satisfy the conditions based on the state, and subsequently, there should be a match in at least one value between the city and credit limit.

Example 3 - Query Output

i

Comments(0 comments)

Comments Not Found