Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in SQL Between operator and ">=" & "<=" operator

Tags:

sql

mysql

oracle

We are using a SQL query to search on the basis of dateFrom and dateTo fields. for that i am using "greater than equal to(>=)" and "less than equal to(<=)" operator to search on the date fields. somewhere i also find that we can also use SQL "Between" operator to do the same. just wanted to confirm that is there any difference when we use "Between" operator and when we used "(>= & <=)" operator.

like image 678
Mahendra Athneria Avatar asked Jun 15 '11 07:06

Mahendra Athneria


People also ask

What does <= mean in SQL?

<= (Less Than or Equal To) (Transact-SQL) - SQL Server | Microsoft Learn. Skip to main content.

Can you use >= in SQL?

You can use the SQL greater than or equal to comparison with other data types, such as VARCHAR and DATETIME.

What is the difference between != And <>?

Most of the databases support both != and <> as not equal comparison operators. <> means either less than or greater than (i.e. not equal to) and was introduced because not all the keyboards used to have the exclamation !


2 Answers

There is no difference.

(x BETWEEN y AND z)

is the same as writing

((x >= y) AND (x <= z))
like image 73
Anthony Faull Avatar answered Sep 18 '22 16:09

Anthony Faull


Modern databases ship with very intelligent query execution optimisers. One of their main features is query transformation. Logically equivalent expressions can usually be transformed into each other. e.g. as Anthony suggested, the BETWEEN operator can be rewritten by Oracle (and MySQL) as two AND-connected comparisons, and vice versa, if BETWEEN isn't just implemented as syntactic sugar.

So even if there would be a difference (in performance), you can be assured that Oracle will very likely choose the better option.

This means that you can freely choose your preference, e.g. because of readability.

Note: it is not always obvious, what's logically equivalent. Query transformation rules become more complex when it comes to transforming EXISTS, IN, NOT EXISTS, NOT IN... But in this case, they are. For more details read the specification (chapter 8.3 between predicate):

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

like image 37
Lukas Eder Avatar answered Sep 16 '22 16:09

Lukas Eder