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
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:
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.
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.
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.
MySQL logical operators examples:
SELECT * FROM table_name WHERE condition1 AND condition2;
How to use AND operator in MySQL:
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;
OR operator in MySQL queries:
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';
Combining conditions with NOT operator in MySQL:
NOT
operator is used to negate a condition, making it true if the original condition is false.SELECT * FROM users WHERE NOT role = 'admin';
Using parentheses in MySQL logical expressions:
SELECT * FROM customers WHERE (country = 'USA' OR country = 'Canada') AND subscription_status = 'active';
Logical operators precedence in MySQL:
SELECT * FROM table_name WHERE condition1 AND (condition2 OR condition3) AND NOT condition4;
Applying logical operators to multiple conditions in MySQL:
SELECT * FROM products WHERE (category = 'Electronics' AND price > 100) OR (category = 'Clothing' AND stock_quantity > 0);
Short-circuit evaluation in MySQL logical expressions:
SELECT * FROM orders WHERE status = 'shipped' OR (status = 'processing' AND payment_received = 1);
Examples of complex conditions with MySQL logical operators:
-- 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);