-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomparison.html
312 lines (294 loc) · 20.3 KB
/
comparison.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>7.2. Comparison Functions and Operators — Presto 323 Documentation</title>
<link rel="stylesheet" href="../_static/presto.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '323',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="7.3. Conditional Expressions" href="conditional.html" />
<link rel="prev" title="7.1. Logical Operators" href="logical.html" />
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-133457846-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-133457846-1');
</script>
</head>
<body>
<div class="header">
<h1 class="heading"><a href="../index.html">
<span>Presto 323 Documentation</span></a></h1>
<h2 class="heading"><span>7.2. Comparison Functions and Operators</span></h2>
</div>
<div class="topnav">
<p class="nav">
<span class="left">
« <a href="logical.html">7.1. Logical Operators</a>
</span>
<span class="right">
<a href="conditional.html">7.3. Conditional Expressions</a> »
</span>
</p>
</div>
<div class="content">
<div class="section" id="comparison-functions-and-operators">
<h1>7.2. Comparison Functions and Operators</h1>
<div class="section" id="comparison-operators">
<h2>Comparison Operators</h2>
<table border="1" class="docutils">
<colgroup>
<col width="16%" />
<col width="84%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operator</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre"><</span></code></td>
<td>Less than</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">></span></code></td>
<td>Greater than</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre"><=</span></code></td>
<td>Less than or equal to</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">>=</span></code></td>
<td>Greater than or equal to</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">=</span></code></td>
<td>Equal</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre"><></span></code></td>
<td>Not equal</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">!=</span></code></td>
<td>Not equal (non-standard but popular syntax)</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="range-operator-between">
<h2>Range Operator: BETWEEN</h2>
<p>The <code class="docutils literal"><span class="pre">BETWEEN</span></code> operator tests if a value is within a specified range.
It uses the syntax <code class="docutils literal"><span class="pre">value</span> <span class="pre">BETWEEN</span> <span class="pre">min</span> <span class="pre">AND</span> <span class="pre">max</span></code>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="mi">3</span> <span class="k">BETWEEN</span> <span class="mi">2</span> <span class="k">AND</span> <span class="mi">6</span><span class="p">;</span>
</pre></div>
</div>
<p>The statement shown above is equivalent to the following statement:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="mi">3</span> <span class="o">>=</span> <span class="mi">2</span> <span class="k">AND</span> <span class="mi">3</span> <span class="o"><=</span> <span class="mi">6</span><span class="p">;</span>
</pre></div>
</div>
<p>To test if a value does not fall within the specified range
use <code class="docutils literal"><span class="pre">NOT</span> <span class="pre">BETWEEN</span></code>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="mi">3</span> <span class="k">NOT</span> <span class="k">BETWEEN</span> <span class="mi">2</span> <span class="k">AND</span> <span class="mi">6</span><span class="p">;</span>
</pre></div>
</div>
<p>The statement shown above is equivalent to the following statement:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="mi">3</span> <span class="o"><</span> <span class="mi">2</span> <span class="k">OR</span> <span class="mi">3</span> <span class="o">></span> <span class="mi">6</span><span class="p">;</span>
</pre></div>
</div>
<p>A <code class="docutils literal"><span class="pre">NULL</span></code> in a <code class="docutils literal"><span class="pre">BETWEEN</span></code> or <code class="docutils literal"><span class="pre">NOT</span> <span class="pre">BETWEEN</span></code> statement is evaluated
using the standard <code class="docutils literal"><span class="pre">NULL</span></code> evaluation rules applied to the equivalent
expression above:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">NULL</span> <span class="k">BETWEEN</span> <span class="mi">2</span> <span class="k">AND</span> <span class="mi">4</span><span class="p">;</span> <span class="c1">-- null</span>
<span class="k">SELECT</span> <span class="mi">2</span> <span class="k">BETWEEN</span> <span class="k">NULL</span> <span class="k">AND</span> <span class="mi">6</span><span class="p">;</span> <span class="c1">-- null</span>
<span class="k">SELECT</span> <span class="mi">2</span> <span class="k">BETWEEN</span> <span class="mi">1</span> <span class="k">AND</span> <span class="k">NULL</span><span class="p">;</span> <span class="c1">-- false</span>
<span class="k">SELECT</span> <span class="mi">8</span> <span class="k">BETWEEN</span> <span class="k">NULL</span> <span class="k">AND</span> <span class="mi">6</span><span class="p">;</span> <span class="c1">-- false</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">BETWEEN</span></code> and <code class="docutils literal"><span class="pre">NOT</span> <span class="pre">BETWEEN</span></code> operators can also be used to
evaluate any orderable type. For example, a <code class="docutils literal"><span class="pre">VARCHAR</span></code>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="s1">'Paul'</span> <span class="k">BETWEEN</span> <span class="s1">'John'</span> <span class="k">AND</span> <span class="s1">'Ringo'</span><span class="p">;</span> <span class="c1">-- true</span>
</pre></div>
</div>
<p>Not that the value, min, and max parameters to <code class="docutils literal"><span class="pre">BETWEEN</span></code> and <code class="docutils literal"><span class="pre">NOT</span>
<span class="pre">BETWEEN</span></code> must be the same type. For example, Presto will produce an
error if you ask it if John is between 2.3 and 35.2.</p>
</div>
<div class="section" id="is-null-and-is-not-null">
<h2>IS NULL and IS NOT NULL</h2>
<p>The <code class="docutils literal"><span class="pre">IS</span> <span class="pre">NULL</span></code> and <code class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span> <span class="pre">NULL</span></code> operators test whether a value
is null (undefined). Both operators work for all data types.</p>
<p>Using <code class="docutils literal"><span class="pre">NULL</span></code> with <code class="docutils literal"><span class="pre">IS</span> <span class="pre">NULL</span></code> evaluates to true:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">select</span> <span class="k">NULL</span> <span class="k">IS</span> <span class="k">NULL</span><span class="p">;</span> <span class="c1">-- true</span>
</pre></div>
</div>
<p>But any other constant does not:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="mi">3</span><span class="p">.</span><span class="mi">0</span> <span class="k">IS</span> <span class="k">NULL</span><span class="p">;</span> <span class="c1">-- false</span>
</pre></div>
</div>
</div>
<div class="section" id="is-distinct-from-and-is-not-distinct-from">
<h2>IS DISTINCT FROM and IS NOT DISTINCT FROM</h2>
<p>In SQL a <code class="docutils literal"><span class="pre">NULL</span></code> value signifies an unknown value, so any comparison
involving a <code class="docutils literal"><span class="pre">NULL</span></code> will produce <code class="docutils literal"><span class="pre">NULL</span></code>. The <code class="docutils literal"><span class="pre">IS</span> <span class="pre">DISTINCT</span> <span class="pre">FROM</span></code>
and <code class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span> <span class="pre">DISTINCT</span> <span class="pre">FROM</span></code> operators treat <code class="docutils literal"><span class="pre">NULL</span></code> as a known value
and both operators guarantee either a true or false outcome even in
the presence of <code class="docutils literal"><span class="pre">NULL</span></code> input:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">NULL</span> <span class="k">IS</span> <span class="k">DISTINCT</span> <span class="k">FROM</span> <span class="k">NULL</span><span class="p">;</span> <span class="c1">-- false</span>
<span class="k">SELECT</span> <span class="k">NULL</span> <span class="k">IS</span> <span class="k">NOT</span> <span class="k">DISTINCT</span> <span class="k">FROM</span> <span class="k">NULL</span><span class="p">;</span> <span class="c1">-- true</span>
</pre></div>
</div>
<p>In the example shown above, a <code class="docutils literal"><span class="pre">NULL</span></code> value is not considered
distinct from <code class="docutils literal"><span class="pre">NULL</span></code>. When you are comparing values which may
include <code class="docutils literal"><span class="pre">NULL</span></code> use these operators to guarantee either a <code class="docutils literal"><span class="pre">TRUE</span></code> or
<code class="docutils literal"><span class="pre">FALSE</span></code> result.</p>
<p>The following truth table demonstrate the handling of <code class="docutils literal"><span class="pre">NULL</span></code> in
<code class="docutils literal"><span class="pre">IS</span> <span class="pre">DISTINCT</span> <span class="pre">FROM</span></code> and <code class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span> <span class="pre">DISTINCT</span> <span class="pre">FROM</span></code>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="13%" />
<col width="15%" />
<col width="15%" />
<col width="19%" />
<col width="26%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">a</th>
<th class="head">b</th>
<th class="head">a = b</th>
<th class="head">a <> b</th>
<th class="head">a DISTINCT b</th>
<th class="head">a NOT DISTINCT b</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">1</span></code></td>
<td><code class="docutils literal"><span class="pre">1</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">1</span></code></td>
<td><code class="docutils literal"><span class="pre">2</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">1</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">NULL</span></code></td>
<td><code class="docutils literal"><span class="pre">FALSE</span></code></td>
<td><code class="docutils literal"><span class="pre">TRUE</span></code></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="greatest-and-least">
<h2>GREATEST and LEAST</h2>
<p>These functions are not in the SQL standard, but are a common extension.
Like most other functions in Presto, they return null if any argument is
null. Note that in some other databases, such as PostgreSQL, they only
return null if all arguments are null.</p>
<p>The following types are supported:
<code class="docutils literal"><span class="pre">DOUBLE</span></code>,
<code class="docutils literal"><span class="pre">BIGINT</span></code>,
<code class="docutils literal"><span class="pre">VARCHAR</span></code>,
<code class="docutils literal"><span class="pre">TIMESTAMP</span></code>,
<code class="docutils literal"><span class="pre">TIMESTAMP</span> <span class="pre">WITH</span> <span class="pre">TIME</span> <span class="pre">ZONE</span></code>,
<code class="docutils literal"><span class="pre">DATE</span></code></p>
<dl class="function">
<dt id="greatest">
<code class="descname">greatest</code><span class="sig-paren">(</span><em>value1</em>, <em>value2</em>, <em>...</em>, <em>valueN</em><span class="sig-paren">)</span> → [same as input]</dt>
<dd><p>Returns the largest of the provided values.</p>
</dd></dl>
<dl class="function">
<dt id="least">
<code class="descname">least</code><span class="sig-paren">(</span><em>value1</em>, <em>value2</em>, <em>...</em>, <em>valueN</em><span class="sig-paren">)</span> → [same as input]</dt>
<dd><p>Returns the smallest of the provided values.</p>
</dd></dl>
</div>
<div class="section" id="quantified-comparison-predicates-all-any-and-some">
<h2>Quantified Comparison Predicates: ALL, ANY and SOME</h2>
<p>The <code class="docutils literal"><span class="pre">ALL</span></code>, <code class="docutils literal"><span class="pre">ANY</span></code> and <code class="docutils literal"><span class="pre">SOME</span></code> quantifiers can be used together with comparison operators in the
following way:</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>expression operator quantifier ( subquery )
</pre></div>
</div>
<p>For example:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="s1">'hello'</span> <span class="o">=</span> <span class="k">ANY</span> <span class="p">(</span><span class="k">VALUES</span> <span class="s1">'hello'</span><span class="p">,</span> <span class="s1">'world'</span><span class="p">);</span> <span class="c1">-- true</span>
<span class="k">SELECT</span> <span class="mi">21</span> <span class="o"><</span> <span class="k">ALL</span> <span class="p">(</span><span class="k">VALUES</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">21</span><span class="p">);</span> <span class="c1">-- false</span>
<span class="k">SELECT</span> <span class="mi">42</span> <span class="o">>=</span> <span class="k">SOME</span> <span class="p">(</span><span class="k">SELECT</span> <span class="mi">41</span> <span class="k">UNION</span> <span class="k">ALL</span> <span class="k">SELECT</span> <span class="mi">42</span> <span class="k">UNION</span> <span class="k">ALL</span> <span class="k">SELECT</span> <span class="mi">43</span><span class="p">);</span> <span class="c1">-- true</span>
</pre></div>
</div>
<p>Here are the meanings of some quantifier and comparison operator combinations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="16%" />
<col width="84%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Expression</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre">=</span> <span class="pre">ALL</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> is equal to all values.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre"><></span> <span class="pre">ALL</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> doesn’t match any value.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre"><</span> <span class="pre">ALL</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> is smaller than the smallest value.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre">=</span> <span class="pre">ANY</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> is equal to any of the values. This form is equivalent to <code class="docutils literal"><span class="pre">A</span> <span class="pre">IN</span> <span class="pre">(...)</span></code>.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre"><></span> <span class="pre">ANY</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> doesn’t match one or more values.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">A</span> <span class="pre"><</span> <span class="pre">ANY</span> <span class="pre">(...)</span></code></td>
<td>Evaluates to <code class="docutils literal"><span class="pre">true</span></code> when <code class="docutils literal"><span class="pre">A</span></code> is smaller than the biggest value.</td>
</tr>
</tbody>
</table>
<p><code class="docutils literal"><span class="pre">ANY</span></code> and <code class="docutils literal"><span class="pre">SOME</span></code> have the same meaning and can be used interchangeably.</p>
</div>
</div>
</div>
<div class="bottomnav">
<p class="nav">
<span class="left">
« <a href="logical.html">7.1. Logical Operators</a>
</span>
<span class="right">
<a href="conditional.html">7.3. Conditional Expressions</a> »
</span>
</p>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>