Toad World® Forums

TDP3.3- Query Builder- OR condition


#1

Hi, All.

I have this query in Query Builder:

SELECT TEMP_A, TEMP_B, TEMP_C
FROM TEMP
WHERE (TEMP_A = 1)
OR (TEMP_C = ‘A’)
AND (TEMP_B = ‘Y’)

It does not return the right values. When I take the SQL code and reformat it to:

SELECT TEMP_A, TEMP_B, TEMP_C
FROM TEMP
WHERE (TEMP_A = 1
OR TEMP_C = ‘A’)
AND (TEMP_B = ‘Y’)

then I get the correct values.

It seems that Query Builder is not handling OR statement properly. Am I missing something here?


#2

In SQL language operation AND is executed before the operation OR because the precedence of operation AND is higher than operation OR. In the second query you use parentheses to explicitly set a different order. That’s why it returns a different result.

It’s always a good idea to use parentheses in complex expression to avoid confusion.


#3

Query builder will not build it that way by selecting the or line on a different field. It will only put the “and” and “or” inside of the same parentheses when you use the" where" and “or” on the same field. Try using the formula tab on the where line of the Temp_A field instead, put your A condition then “or” and your Temp_B condition.