Priority of AND and OR operator in MySQL select query?

MySQLMySQLi Database

The AND has the highest priority than OR operator in MySQL select query.

Let us check how MySQL gives the highest priority to AND operator.

The query is as follows

mysql> select 0 AND 0 OR 1 as Result;

The following is the output

+--------+
| Result |
+--------+
| 1      |
+--------+
1 row in set (0.00 sec)

If you are considering the OR operator has the highest priority then MySQL will wrap up the above query like this.

The query is as follows

select 0 AND (0 OR 1) as Result

First, solve the 0 OR 1, this will give result 1. After that 0 and 1 will give result 0.

But the above case is not fine because we are getting 0 and output is 1. So, in the above query AND gets highest priority than OR to get result 1.

The query is as follows

select 0 AND 0 OR 1 as Result

First, solve AND operator first. 0 AND 0 gives result 1.

After that 1 OR 1 gives result 1.

Now we are getting the exact output.

raja
Published on 29-Mar-2019 06:28:27
Advertisements