sql: BETWEEN v1 AND v2

倖福魔咒の 提交于 2019-12-01 18:18:55
Tom Ritter

SQL Server 2008:

select 1 
where 5 between 1 and 7

1 result

select 1 
where 5 between 7 and 1

0 results

Based on these results, and the Postgre Docs I would hypothesize that the ANSI Standard is as follows (although I can't find that doc).

a between x and y
==
a >= x AND a <= y

UPDATE:

The SQL-92 spec says (quote):

"X BETWEEN Y AND Z" is equivalent to "X>=Y AND X<=Z"

Yes! The order of the arguments in the BETWEEN predicate matter. And, yes, this is [mostly] syntactic sugar for the AND-ed comparison form.

The "mostly", above comes from the fact that while logically equivalent, the two expressions may (probably in the past...) receive a distinct query plan on some SQL servers. It is a safe guess that most servers, nowadays, provide an optimal handling of this type of filter, regardless of its form.

Yes, you are right, is it only syntactic sugar for the construct you mentioned.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!