Skip to content

Commit 61e7ffe

Browse files
committed
Add Between Symmetric as a postgres til
1 parent 06c0f0e commit 61e7ffe

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_503 TILs and counting..._
10+
_504 TILs and counting..._
1111

1212
---
1313

@@ -220,6 +220,7 @@ _503 TILs and counting..._
220220
- [Aggregate A Column Into An Array](postgres/aggregate-a-column-into-an-array.md)
221221
- [Assumed Radius Of The Earth](postgres/assumed-radius-of-the-earth.md)
222222
- [Auto Expanded Display](postgres/auto-expanded-display.md)
223+
- [Between Symmetric](postgres/between-symmetric.md)
223224
- [Change The Current Directory For psql](postgres/change-the-current-directory-for-psql.md)
224225
- [Checking Inequality](postgres/checking-inequality.md)
225226
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)

postgres/between-symmetric.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Between Symmetric
2+
3+
PostgreSQL's `between` construct allows you to make a comparison _between_
4+
two values (numbers, timestamps, etc.).
5+
6+
```sql
7+
> select * from generate_series(1,10) as numbers(a) where numbers.a between 3 and 6;
8+
a
9+
---
10+
3
11+
4
12+
5
13+
6
14+
```
15+
16+
If you supply an empty range by using the larger of the two values first, an
17+
empty set will result.
18+
19+
```sql
20+
> select * from generate_series(1,10) as numbers(a) where numbers.a between 6 and 3;
21+
a
22+
---
23+
```
24+
25+
Tacking `symmetric` onto the `between` construct is one way to avoid this
26+
issue.
27+
28+
```sql
29+
> select * from generate_series(1,10) as numbers(a) where numbers.a between symmetric 6 and 3;
30+
a
31+
---
32+
3
33+
4
34+
5
35+
6
36+
```
37+
38+
> BETWEEN SYMMETRIC is the same as BETWEEN except there is no requirement
39+
> that the argument to the left of AND be less than or equal to the argument
40+
> on the right. If it is not, those two arguments are automatically swapped,
41+
> so that a nonempty range is always implied.

0 commit comments

Comments
 (0)