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

前端 未结 11 834
孤独总比滥情好
孤独总比滥情好 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 17:59

    Semantically, the two expressions have the same result.

    However, BETWEEN is a single predicate, instead of two comparison predicates combined with AND. Depending on the optimizer provided by your RDBMS, a single predicate may be easier to optimize than two predicates.

    Although I expect most modern RDBMS implementations should optimize the two expressions identically.

    0 讨论(0)
  • 2020-12-09 17:59

    worse if it's

      SELECT id FROM entries 
      WHERE 
         (SELECT COUNT(id) FROM anothertable WHERE something LEFT JOIN something ON...) 
         BETWEEN entries.max AND entries.min;
    

    Rewrite this one with your syntax without using temporary storage.

    0 讨论(0)
  • 2020-12-09 18:05

    The version with "between" is easier to read. If I were to use the second version I'd probably write it as

    5000 <= salary and salary <= 15000
    

    for the same reason.

    0 讨论(0)
  • 2020-12-09 18:05

    If the endpoints are inclusive, then BETWEEN is the preferred syntax.

    Less references to a column means less spots to update when things change. It's the engineering principle, that less things means less stuff can break.

    It also means less possibility of someone putting the wrong bracket for things like including an OR. IE:

    WHERE salary BETWEEN 5000 AND (15000
      OR ...)
    

    ...you'll get an error if you put the bracket around the AND part of a BETWEEN statement. Versus:

    WHERE salary >= 5000
     AND (salary <= 15000
      OR ...)
    

    ...you'd only know there's a problem when someone reviews the data returned from the query.

    0 讨论(0)
  • 2020-12-09 18:06

    Using BETWEEN has extra merits when the expression that is compared is a complex calculation rather than just a simple column; it saves writing out that complex expression twice.

    0 讨论(0)
  • 2020-12-09 18:09

    I vote @Quassnoi - correctness is a big win.

    I usually find literals more useful than the syntax symbols like <, <=, >, >=, != etc. Yes, we need (better, accurate) results. And at least I get rid of probabilities of mis-interpreting and reverting meanings of the symbols visually. If you use <= and sense logically incorrect output coming from your select query, you may wander some time and only arrive to the conclusion that you did write <= in place of >= [visual mis-interpretation?]. Hope I am clear.

    And aren't we shortening the code (along with making it more higher-level-looking), which means more concise and easy to maintain?

    SELECT * 
    FROM emplyees 
    WHERE salary between 5000 AND 15000; 
    
    
    
    SELECT * 
    FROM emplyees 
    WHERE salary >= 5000 AND salary <= 15000; 
    

    First query uses only 10 words and second uses 12!

    0 讨论(0)
提交回复
热议问题