Why use the BETWEEN operator when we can do without it?

前端 未结 11 840
孤独总比滥情好
孤独总比滥情好 2020-12-09 17:31

As seen below the two queries, we find that they both work well. Then I am confused why should we ever use BETWEEN because I have found that BETWEEN behaves differently in d

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 18:23

    In SQL, I agree that BETWEEN is mostly unnecessary, and can be emulated syntactically with 5000 <= salary AND salary <= 15000. It is also limited; I often want to apply an inclusive lower bound and an exclusive upper bound: @start <= when AND when < @end, which you can't do with BETWEEN.

    OTOH, BETWEEN is convenient if the value being tested is the result of a complex expression.

    It would be nice if SQL and other languages would follows Python's lead in using proper mathematical notation: 5000 <= salary <= 15000.

    One small tip that wil make your code more readable: use < and <= in preference to > and >=.

提交回复
热议问题