ALL VS operator Any of an empty request

I read oracle documentation for ANY and EVERY operators. I quite understand their use, except for one. It states:

ALL:

If the subquery returns zero rows, the condition is TRUE.

ANY:

If the subquery returns zero rows, the condition is FALSE.

It seems to me not very logical. Why does ALL return TRUE on an empty subquery, but ANY returns FALSE?

I'm relatively new to SQL, so I guess it will have a precedent for this behavior, which is completely intuitive to me.

ANYONE and EVERYONE on an empty set should return the same no value?

+5
source share
2 answers

Because ANY should be interpreted as EXIST (if there is, then they exist). Therefore, it returns false if no rows are found.

All does not confirm that there are any meanings; it simply confirms that it represents all possible meanings. Therefore, it returns true even if no rows are found.

+4
source

Let's EMP at an example EMP table in this link.

In particular, this request is

 SELECT e1.empno, e1.sal FROM emp e1 WHERE e1.sal > ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 20); 

In the case of ANY question, you ask the question: "Is my salary greater than anyone in department 20 (at least 1 person)." This means that you hope that at least one person has a lower salary than you. When there are no rows, this returns FALSE , because there is no one whose salary is less than you, you hoped for at least one.

In the case of EVERYTHING, the obvious question you ask is, "Is my salary the most?" To paraphrase this as "Does anyone have a bigger salary than me?" When there are no returned rows, your answer is TRUE , because "there is no one whose salary is higher than me.

+5
source

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


All Articles