MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL logical operators

Logical operators in MySQL are used to combine or negate conditions in a SQL statement. They are primarily used in the WHERE clause to filter the results of a SELECT, INSERT, UPDATE, or DELETE statement.

Here are the most common logical operators used in MySQL:

  • AND: Returns true if all the conditions separated by AND are true.
  • OR: Returns true if at least one of the conditions separated by OR is true.
  • NOT: Returns true if the condition(s) following the NOT is false.

Examples:

  • AND
SELECT * FROM employees WHERE salary > 50000 AND department_id = 1;

This will select all the data from the employees table where salary is greater than 50000 and the department_id is 1. Both conditions must be true for any row to be included in the result.

  • OR
SELECT * FROM employees WHERE salary > 50000 OR department_id = 1;

This will select all the data from the employees table where salary is greater than 50000 or the department_id is 1. If either condition is true, the row will be included in the result.

  • NOT
SELECT * FROM employees WHERE NOT salary > 50000;

This will select all the data from the employees table where salary is not greater than 50000. In other words, it will return the rows where salary is 50000 or less.

You can also use brackets () to group conditions and control the order of logical operations. This can be useful when you have more complex queries with multiple AND and OR conditions.

For example:

SELECT * FROM employees WHERE (salary > 50000 OR department_id = 1) AND age < 30;

In this query, the OR operation is performed first because it is inside the brackets. The AND operation is then performed on the result. Therefore, this query will select all employees who are under 30 and who either have a salary greater than 50000 or belong to department 1.

  1. MySQL logical operators examples:

    • MySQL provides logical operators (AND, OR, NOT) for combining and negating conditions in queries.
    SELECT * FROM table_name WHERE condition1 AND condition2;
    
  2. How to use AND operator in MySQL:

    • The AND operator is used to combine multiple conditions, and all conditions must be true for the overall condition to be true.
    SELECT * FROM products WHERE price > 50 AND stock_quantity > 0;
    
  3. OR operator in MySQL queries:

    • The OR operator is used to combine multiple conditions, and at least one of the conditions must be true for the overall condition to be true.
    SELECT * FROM orders WHERE status = 'shipped' OR status = 'delivered';
    
  4. Combining conditions with NOT operator in MySQL:

    • The NOT operator is used to negate a condition, making it true if the original condition is false.
    SELECT * FROM users WHERE NOT role = 'admin';
    
  5. Using parentheses in MySQL logical expressions:

    • Parentheses can be used to control the order of evaluation in complex logical expressions.
    SELECT * FROM customers WHERE (country = 'USA' OR country = 'Canada') AND subscription_status = 'active';
    
  6. Logical operators precedence in MySQL:

    • MySQL follows the standard logical operator precedence: NOT, AND, OR. Use parentheses to explicitly define the order.
    SELECT * FROM table_name WHERE condition1 AND (condition2 OR condition3) AND NOT condition4;
    
  7. Applying logical operators to multiple conditions in MySQL:

    • Combine logical operators to create complex conditions based on multiple criteria.
    SELECT * FROM products WHERE (category = 'Electronics' AND price > 100) OR (category = 'Clothing' AND stock_quantity > 0);
    
  8. Short-circuit evaluation in MySQL logical expressions:

    • MySQL uses short-circuit evaluation, meaning it stops evaluating conditions as soon as the overall result is determined.
    SELECT * FROM orders WHERE status = 'shipped' OR (status = 'processing' AND payment_received = 1);
    
  9. Examples of complex conditions with MySQL logical operators:

    • Applying logical operators to create sophisticated conditions for data retrieval.
    -- Example 1: Combining AND, OR, and NOT
    SELECT * FROM employees WHERE department = 'HR' AND (salary > 50000 OR years_of_service > 5) AND NOT is_contractor;
    
    -- Example 2: Using parentheses for clarity
    SELECT * FROM products WHERE (category = 'Electronics' AND price > 100) OR (category = 'Clothing' AND stock_quantity > 0);