Postgresql and brackets

I always use parentheses in sql queries. But I have an example:

DELETE FROM prog WHERE prog_start >= $1 AND prog_start < $2 OR prog_end > $1 AND prog_end <= $2 

It is equal to:

 DELETE FROM prog WHERE ( prog_start >= $1 AND prog_start < $2 ) OR ( prog_end > $1 AND prog_end <= $2 ) 

or not?

+6
source share
2 answers

It depends on the "priority of logical operators" in the language.

In postgresql, the AND operator takes precedence over the OR operator

So, in your case, the result will be the same.

But I think it is much simpler, and clear (service!) To put parentheses.

+8
source

This happens according to Operator Priority http://www.postgresql.org/docs/6.5/static/operators.htm#AEN1615 .

To form a complex condition, it is always best to copy your conditions.

+1
source

Source: https://habr.com/ru/post/914212/


All Articles