Skip to content

Commit 93e82a5

Browse files
Add operator documentation (BETWEEN, LIKE, IN, IS DISTINCT FROM) (#3762)
## Summary Adds complete documentation for SQL operators: BETWEEN, LIKE, IN, and IS DISTINCT FROM. ## Operators documented - **BETWEEN** - Range testing with inclusive bounds - **LIKE** - Pattern matching with wildcards (%, _) and ESCAPE clause - **IN** - Value list membership testing - **IS DISTINCT FROM** - NULL-safe equality comparison ## Files - BETWEEN.rst/diagram with comprehensive examples - LIKE.rst/diagram with wildcard patterns and ESCAPE - IN.rst/diagram with NULL handling details - Updated Comparison.rst with IS DISTINCT FROM - Updated Operators.rst index - operators-documentation-queries.yamsql test file - Updated DocumentationQueriesTests.java
1 parent 23a1fd2 commit 93e82a5

File tree

17 files changed

+1707
-11
lines changed

17 files changed

+1707
-11
lines changed

docs/sphinx/source/reference/sql_commands/DQL/Operators.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Operators
88
Operators/Logical
99
Operators/Comparison
1010
Operators/IS
11+
Operators/IS_DISTINCT_FROM
12+
Operators/BETWEEN
13+
Operators/LIKE
14+
Operators/IN
1115

1216
.. list-table::
1317

@@ -19,3 +23,11 @@ Operators
1923
- `<`, `>`, `<=`, `>=`, `=`, `!=`
2024
* - :ref:`IS Operator <is-operators>`
2125
- `IS`, `IS NOT`
26+
* - :ref:`IS DISTINCT FROM Operator <is-distinct-from>`
27+
- `IS DISTINCT FROM`, `IS NOT DISTINCT FROM`
28+
* - :ref:`BETWEEN Operator <between>`
29+
- `BETWEEN`, `NOT BETWEEN`
30+
* - :ref:`LIKE Operator <like>`
31+
- `LIKE`, `NOT LIKE`
32+
* - :ref:`IN Operator <in>`
33+
- `IN`, `NOT IN`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Diagram(
2+
NonTerminal('expression'),
3+
Optional(Terminal('NOT'), 1),
4+
Terminal('BETWEEN'),
5+
NonTerminal('lower_bound'),
6+
Terminal('AND'),
7+
NonTerminal('upper_bound')
8+
)
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
=======
2+
BETWEEN
3+
=======
4+
5+
.. _between:
6+
7+
Tests whether a value falls within a specified range (inclusive).
8+
9+
Syntax
10+
======
11+
12+
.. raw:: html
13+
:file: BETWEEN.diagram.svg
14+
15+
The BETWEEN operator is used in WHERE clauses:
16+
17+
.. code-block:: sql
18+
19+
SELECT column1, column2
20+
FROM table_name
21+
WHERE column1 BETWEEN lower_value AND upper_value
22+
23+
Parameters
24+
==========
25+
26+
``expression``
27+
The value to test. Can be a column name, calculation, or any valid expression.
28+
29+
``lower_bound``
30+
The lower bound of the range (inclusive).
31+
32+
``upper_bound``
33+
The upper bound of the range (inclusive).
34+
35+
``NOT`` (optional)
36+
Negates the result - returns true if the value is **outside** the range.
37+
38+
Returns
39+
=======
40+
41+
Returns:
42+
- ``TRUE`` if ``expression >= lower_bound AND expression <= upper_bound``
43+
- ``FALSE`` otherwise
44+
- ``NULL`` if any operand is NULL
45+
46+
With ``NOT BETWEEN``:
47+
- ``TRUE`` if ``expression < lower_bound OR expression > upper_bound``
48+
- ``FALSE`` otherwise
49+
- ``NULL`` if any operand is NULL
50+
51+
**Important**: If ``lower_bound > upper_bound``, the range is empty and ``BETWEEN`` always returns ``FALSE``.
52+
53+
Examples
54+
========
55+
56+
Setup
57+
-----
58+
59+
For these examples, assume we have a ``products`` table:
60+
61+
.. code-block:: sql
62+
63+
CREATE TABLE products(
64+
id BIGINT,
65+
name STRING,
66+
price BIGINT,
67+
stock INTEGER,
68+
PRIMARY KEY(id))
69+
70+
INSERT INTO products VALUES
71+
(1, 'Widget A', 100, 50),
72+
(2, 'Widget B', 150, 30),
73+
(3, 'Gadget X', 200, 20),
74+
(4, 'Tool A', 80, 100),
75+
(5, 'Tool B', 120, 15)
76+
77+
BETWEEN with Numbers
78+
--------------------
79+
80+
Find products with prices between 100 and 150 (inclusive):
81+
82+
.. code-block:: sql
83+
84+
SELECT name, price
85+
FROM products
86+
WHERE price BETWEEN 100 AND 150
87+
88+
.. list-table::
89+
:header-rows: 1
90+
91+
* - :sql:`name`
92+
- :sql:`price`
93+
* - :json:`"Widget A"`
94+
- :json:`100`
95+
* - :json:`"Widget B"`
96+
- :json:`150`
97+
* - :json:`"Tool B"`
98+
- :json:`120`
99+
100+
NOT BETWEEN
101+
-----------
102+
103+
Find products with prices outside the range 100-150:
104+
105+
.. code-block:: sql
106+
107+
SELECT name, price
108+
FROM products
109+
WHERE price NOT BETWEEN 100 AND 150
110+
111+
.. list-table::
112+
:header-rows: 1
113+
114+
* - :sql:`name`
115+
- :sql:`price`
116+
* - :json:`"Tool A"`
117+
- :json:`80`
118+
* - :json:`"Gadget X"`
119+
- :json:`200`
120+
121+
BETWEEN with Equal Bounds
122+
--------------------------
123+
124+
Test for exact value using BETWEEN:
125+
126+
.. code-block:: sql
127+
128+
SELECT name, price
129+
FROM products
130+
WHERE price BETWEEN 100 AND 100
131+
132+
.. list-table::
133+
:header-rows: 1
134+
135+
* - :sql:`name`
136+
- :sql:`price`
137+
* - :json:`"Widget A"`
138+
- :json:`100`
139+
140+
This is equivalent to ``WHERE price = 100``.
141+
142+
Empty Range
143+
-----------
144+
145+
If lower bound > upper bound, no rows match:
146+
147+
.. code-block:: sql
148+
149+
SELECT name, price
150+
FROM products
151+
WHERE price BETWEEN 150 AND 100
152+
153+
Returns empty result set (no rows).
154+
155+
Combined with OR
156+
----------------
157+
158+
Use multiple BETWEEN clauses with OR:
159+
160+
.. code-block:: sql
161+
162+
SELECT name, price
163+
FROM products
164+
WHERE price BETWEEN 80 AND 100 OR price BETWEEN 180 AND 220
165+
166+
.. list-table::
167+
:header-rows: 1
168+
169+
* - :sql:`name`
170+
- :sql:`price`
171+
* - :json:`"Tool A"`
172+
- :json:`80`
173+
* - :json:`"Widget A"`
174+
- :json:`100`
175+
* - :json:`"Gadget X"`
176+
- :json:`200`
177+
178+
Important Notes
179+
===============
180+
181+
Inclusive Range
182+
---------------
183+
184+
``BETWEEN`` uses **inclusive** bounds. Both ``lower_bound`` and ``upper_bound`` are included in the matching range.
185+
186+
NULL Handling
187+
-------------
188+
189+
If any operand (expression, lower_bound, or upper_bound) is NULL, the result is NULL:
190+
191+
.. code-block:: sql
192+
193+
-- Returns NULL
194+
WHERE NULL BETWEEN 1 AND 10
195+
196+
-- Returns NULL
197+
WHERE price BETWEEN NULL AND 100
198+
199+
-- Returns NULL
200+
WHERE price BETWEEN 100 AND NULL
201+
202+
Equivalence
203+
-----------
204+
205+
``BETWEEN`` is shorthand for a range check:
206+
207+
.. code-block:: sql
208+
209+
-- These are equivalent:
210+
WHERE x BETWEEN a AND b
211+
WHERE x >= a AND x <= b
212+
213+
-- These are equivalent:
214+
WHERE x NOT BETWEEN a AND b
215+
WHERE x < a OR x > b
216+
217+
Type Compatibility
218+
------------------
219+
220+
The expression, lower_bound, and upper_bound must be of compatible types. The comparison follows SQL type coercion rules.
221+
222+
See Also
223+
========
224+
225+
* :ref:`Comparison Operators <comparison-operators>` - Other comparison operations

docs/sphinx/source/reference/sql_commands/DQL/Operators/Comparison.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,35 @@
22
Comparison Operators
33
====================
44

5-
65
.. _comparison-operators:
76

8-
TODO: flesh out doc
7+
Standard Comparison Operators
8+
==============================
9+
10+
We support standard comparison operators: ``>``, ``>=``, ``=``, ``<``, ``<=``, ``!=``, ``<>``
11+
12+
If either the left or right operand is NULL, the comparison result is NULL.
13+
14+
.. code-block:: sql
15+
16+
SELECT * FROM products WHERE price > 100
17+
SELECT * FROM products WHERE price <= 50
18+
SELECT * FROM products WHERE name = 'Widget'
19+
SELECT * FROM products WHERE stock != 0
20+
21+
IS DISTINCT FROM
22+
================
23+
24+
NULL-safe comparison operator that treats NULL values as comparable:
25+
26+
- ``expression1 IS DISTINCT FROM expression2`` - Returns ``TRUE`` if values are different (treating NULL as a value)
27+
- ``expression1 IS NOT DISTINCT FROM expression2`` - Returns ``TRUE`` if values are the same (treating NULL as a value)
28+
29+
Unlike ``=`` and ``!=``, this operator never returns NULL.
30+
31+
For detailed documentation, examples, and use cases, see :ref:`IS DISTINCT FROM <is-distinct-from>`.
932

10-
We support comparison operators: :sql:`>`, :sql:`>=`, :sql:`=`, :sql:`<`, :sql:`<=`, :sql:`!=`
33+
See Also
34+
========
1135

12-
If the left or right operand is :sql:`NULL`, the comparison result is :sql:`NULL`.
36+
* :ref:`IS Operator <is-operators>` - IS NULL, IS TRUE, IS FALSE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Diagram(
2+
NonTerminal('expression'),
3+
Optional(Terminal('NOT'), 1),
4+
Terminal('IN'),
5+
Terminal('('),
6+
OneOrMore(
7+
NonTerminal('value'),
8+
Terminal(',')
9+
),
10+
Terminal(')')
11+
)

0 commit comments

Comments
 (0)