×

MySQL logical conditions

MySQL logical conditions

Introduction

MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions or queries.

The logical condition works on two expressions of the data. It supports Boolean conditions. The logical condition comes up with the "WHERE" clause. The logical operator works with where clause and conditions.

Logical ConditionExpression
MySQL AND conditionThis condition becomes true when both conditions are true.
MySQL OR conditionThis condition becomes true when at least the condition comes true.
MySQL AND OR conditionMySQL query uses "AND" with the "OR" condition in the "WHERE" clause.
MySQL NOT conditionThis condition inverses the expression. Display negative output of the given query.

Syntax

The basic logical operators work between two MySQL queries. The logical condition syntax is below.

(Mysql query1) logical_condition (mysql query2);

The logical condition uses MySQL expression and logical operators with the "WHERE" clause. The basic syntax of the logical operator is below.

SELECT columns FROM table_name WHERE query1 logical_condition query2;

You can add the "AND," "OR," "NOT," "AND OR" keyword as a logical condition. You can select, update, and delete statements using the above syntax.

The Boolean data type is also a logical condition. Here, the Boolean condition uses the TINYTEXT data type. The true value shows as "1," and the false value shows "0".

MySQL AND condition

MySQL "AND" condition returns true when both queries are valid. If one query is false, then the output becomes false. This condition helps to retrieve, delete, modify data.

  First QuerySecond queryOutput
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

The above table displays the working procedure of the AND condition.  You use this operator on more than two queries. Here, all queries must be valid, otherwise false.

Syntax

The AND condition requires a minimum of two queries to fulfill operation. The "AND" operator chooses more than two queries. The "AND" condition syntax is below.

 MySQL "AND" condition uses single time with two queries.
 SELECT columns [*] FROM table_name WHERE query1 AND query2;
 MySQL "AND" condition is used multiple times with various queries.
 SELECT columns [*] FROM table_name WHERE query1 AND query2 AND query3; 

Examples of the "AND" condition

1) Example: The "AND" condition uses a single time.

Execute the below query to get the required data from the table. Here, two query needs to use AND operator.

 mysql> select topic, time, level 
 from mysql_tutorial
 WHERE topic = 'mysql query' AND time = '1 hour'; 

OUTPUT

MySQL logical conditions

Here, operators choose topic = 'mysql query' AND time = '1 hour'. Then, you get the third row of the table. The "AND" condition fulfills both conditions then displays the output.

2) Example: The "AND" condition is used multiple times.

Execute the below query to get the required data from the table. Here, more than two queries work with multiple operators.

 mysql> select topic, time, level 
 from mysql_tutorial
 WHERE topic = 'mysql introduction' AND time = '45 minutes' AND level ='beginners'; 

OUTPUT

MySQL logical conditions

Here, operators choose the WHERE clause, multiple queries, and the "AND" operators. Then, you get the first row of the table. The "AND" condition fulfills both conditions then displays the output.

3) Example: The "AND" condition does not fulfill in a query.

Execute the below query to get the required output.

 mysql> select topic, time, level
 from mysql_tutorial
 WHERE index_number = 2 AND level = "intermediate"; 

OUTPUT

MySQL logical conditions

If both conditions are not fulfilled then, the output displays "Empty set." Here, we know about the false condition.

MySQL OR condition

MySQL "OR" condition returns true when at least one query is true. If one query is false, then the output becomes true. If both queries are false, then the output becomes false.

  First QuerySecond queryOutput
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

The above table displays the working procedure of the OR condition.  You use an operator on more than two queries.

Syntax

The OR condition requires a minimum of two queries to fulfill operation. You choose more than two queries.

MySQL "OR" condition uses single time with two queries. The single OR operator syntax shows below.

SELECT columns [*] FROM table_name WHERE query1 OR query2;

MySQL "AND" condition is used multiple times with various queries. The multiple OR operator syntax shows below.

SELECT columns [*] FROM table_name WHERE query1 OR query2 OR query3;

Examples of the "OR" condition

1) Example: The "OR" condition uses a single time.

Execute the below query to know the working procedure of the "OR" operator.

mysql> select topic, time, level  from mysql_tutorial WHERE topic = 'mysql query' OR time = 'half hour';

OUTPUT

MySQL logical conditions

You get the two rows of the table. The first condition fulfills, and the second condition also fulfills.

2) Example: The "OR" condition is used multiple times.

Execute the below query to know the working procedure of the "OR" operator. The condition uses more than two conditions and "OR" operators.

 mysql> select topic, time, level 
 from mysql_tutorial
 WHERE topic = 'mysql query' OR time = 'half hour' OR level = 'beginners'; 

OUTPUT

MySQL logical conditions

You get the three rows of the table. All conditions are fulfilled using "OR" operators. Here, the query uses a two-time "OR" condition. 

3) Example: The "OR" condition is used multiple times.

Execute the below query to know the working procedure of the "OR" operator. The condition uses more than two conditions and "OR" operators.

mysql> select topic, time, level  from mysql_tutorial WHERE topic = 'mysql' OR time = 'half hour' OR level = 'expert';

OUTPUT

MySQL logical conditions

Here, one condition fulfills, and the other two conditions are false. Here, the query uses a two-time "OR" condition. The table only displays time = "half hour" information. Mostly, the "OR" condition does not display false output.

MySQL AND OR condition

In this condition, you use both "AND" and "OR" logical operators.  Sometimes, you need to filter data with detail. MySQL query returns the specifically required information. The "AND" operator and "OR" operators are used in a single MySQL expression.

 Firstly, you should use AND operator as a first query. Secondly, you should use the OR operator as a second query. The query must be in a simple bracket.

Syntax

The "AND OR" condition requires a minimum of two queries to fulfill operation. You can choose more than two queries.

 MySQL "AND OR" condition uses single time with two queries.
 SELECT columns [*] FROM table_name WHERE (query1 AND query2) OR (query3 condition);
 MySQL "OR" condition is used multiple times with various queries.
 SELECT columns [*] FROM table_name WHERE (query1 AND query2) OR (query3 OR query4);
 MySQL "AND" condition is used multiple times with various queries.
 SELECT columns [*] FROM table_name WHERE (query1 AND query2 AND query3) OR (query4); 

Examples of the "AND OR" condition

1) Example: The "AND OR" condition uses a single time.

Execute the below query to know about the "AND OR" operator. If you want to apply the advanced condition, then the "AND" operator and "OR" operator combine.

mysql> select index_number, topic, time, level  from mysql_tutorial WHERE (topic = 'mysql query' AND time = '1 hour') OR (index_number < 2);

OUTPUT

MySQL logical conditions

The "AND OR" operator is used once and gets output above. The "AND" condition and "OR" condition fulfill the above two rows display.

2) Example: The "AND OR" query uses AND condition multiple times.

Executing the below query uses the "AND" operator multiple times.

 mysql> select topic, time, level 
 from mysql_tutorial
 WHERE (index_number = 3 AND topic = 'mysql query' AND time = '1 hour')
 OR ( level = 'begineers'); 

OUTPUT

MySQL logical conditions

You get the above output. Here, you use the "OR" operator one time and two times the "AND" operator. This row fulfills "AND" and "OR" conditions simultaneously.

3) Example: The "AND OR" query uses OR condition multiple times.

Execute the below query using the "OR" operator multiple times.

 mysql> select topic, time, level 
 from mysql_tutorial
 WHERE (index_number = 3 AND topic = 'mysql query')
 OR (time = 'half hour' OR level = 'begineers'); 

OUTPUT

MySQL logical conditions

You get the above output. Here, you use the once "AND" operator and two times "OR" operator. The first row fulfills the "OR" condition. The second row fulfills the "AND" condition.

MySQL NOT condition

Sometimes, you have to filter not required data to operate data. MySQL "NOT" condition inverse the given statement. The "NOT" condition is used with clauses or operators. This condition returns negative.

  Output"NOT" output
TrueFalse
FalseTrue

Syntax

The Not condition requires only one query to fulfill operation. The condition uses clauses and operators to negate MySQL expressions. MySQL "NOT" logical condition works in MySQL expressions.

 SELECT columns [*] FROM table_name WHERE NOT condition (values);
 It mostly uses "IN," "LIKE," "NULL" conditions with multiple values. 

Examples of the "NOT" condition

1) Example: The "NOT" condition with the "IN" condition.

Executing the below query uses the "NOT" operator with the "IN" operator.

 mysql>  select topic, time, level 
 from mysql_tutorial
 WHERE index_number NOT IN (1, 3, 5); 

OUTPUT

MySQL logical conditions

The above table displays only one row. This row does not include the "IN" operator.

2) Example: The "NOT" condition with the "LIKE" condition.

Execute the below query using the "NOT" operator with the "LIKE" operator.

mysql> select topic, time, level  from mysql_tutorial WHERE topic NOT LIKE '%query%';

OUTPUT

MySQL logical conditions

The above table displays only one row. This row does not include the "query" word in the "LIKE" pattern. 

3) Example: The "NOT" condition with the "NULL" condition.

Execute the below query using the "NOT" operator with "IS" and "NULL" operators.

mysql> select * from mysql_tutorial Where topic IS NOT NULL;

OUTPUT

MySQL logical conditions

The above output shows the entire data of the table. The table does not show the null value.

4) Example: The "NOT" condition with the "Between" condition.

Execute the below query using the "NOT" operator with "BETWEEN" and "AND" operators.

mysql> select index_number, a topic from mysql_tutorial Where index_number NOT BETWEEN 1 AND 2;

OUTPUT

MySQL logical conditions

The above output shows the data of the table. Here, the query applies conditions for the first and second index numbers. The NOT condition displays the third row.

Here you know about the "AND," "OR," "AND OR," and "NOT" condition and their working procedure. You get the difference between all logical conditions. MySQL needs a logical condition to search advanced queries.


Related Topics

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL INSERT() Function

In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples. Introduction of MySQL INSERT() function Basically, in MySQL, the INSERT function is...

2 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.