Splunk Search

## What is the order that executes in query when I use both AND, OR in splunk query?

Builder

I have two different queries like below

Query 1 :-

``````field_1!="A" AND field_2="B" OR field_1!="A" AND field_2="C" OR field_1!="A" AND field_2="D" OR field_1!="A" AND field_2="E"
``````

Query 2 :-

``````field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )
``````

In my view both the queries should give the same result but it's giving me the different result. What's the difference between both the queries and what's the actual order splunk executes?

Tags (3)
1 Solution SplunkTrust

Your query 1 is hochpoch because of no clear distinction of the grouping of AND conditions and due to order in which the logical operators are executed (see here) they're not doing what you want to do, which is query 2. Below two query should give you same result.

Reformatted query 1:

``````(field_1!="A" AND field_2="B") OR (field_1!="A" AND field_2="C") OR (field_1!="A" AND field_2="D") OR (field_1!="A" AND field_2="E" )
``````

Query 2 :-

`````` field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )
`````` SplunkTrust

Your query 1 is hochpoch because of no clear distinction of the grouping of AND conditions and due to order in which the logical operators are executed (see here) they're not doing what you want to do, which is query 2. Below two query should give you same result.

Reformatted query 1:

``````(field_1!="A" AND field_2="B") OR (field_1!="A" AND field_2="C") OR (field_1!="A" AND field_2="D") OR (field_1!="A" AND field_2="E" )
``````

Query 2 :-

`````` field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )
``````
Builder

Cool thanks @somesoni2. That makes sense now. PLease post the same as a answer. I am going to accept that. Thanks again.  