Priority of AND and OR operator in MySQL select query?


The AND has the highest priority than the 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 the result 0.

But the above case is not fine because we are getting 0 and the output is 1. So, in the above query, AND gets the 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 give result 0.

After that 0 OR 1 gives result 1.

Now we are getting the exact output.

Updated on: 06-Aug-2021

907 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements