SQL BETWEEN SYMMETRIC operator
BETWEEN that swaps its bounds when the first is larger than the second.
- 类别
- 条件
- Returns
boolean- In this category
- 24 of 43
语法
x BETWEEN SYMMETRIC low AND high
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | 是 | 仅支持 PostgreSQL |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
示例
PostgreSQL
SELECT * FROM t WHERE n BETWEEN SYMMETRIC 500 AND 100;
PostgreSQL
SELECT * FROM t WHERE n BETWEEN LEAST(500, 100) AND GREATEST(500, 100);
值得了解
- Plain BETWEEN with reversed bounds is always FALSE; SYMMETRIC normalizes them first.
- It can hide data bugs: a swapped lower/upper bound that would have returned nothing now silently widens the match.
- Portable form: x BETWEEN LEAST(a, b) AND GREATEST(a, b), but check your engine's NULL semantics first.
Questions about BETWEEN SYMMETRIC
What does SYMMETRIC mean here?
The bounds are symmetric: the engine swaps them if needed, so low > high still matches the range.
Which engines support it?
PostgreSQL only among the big five; elsewhere use LEAST and GREATEST around the bounds.
Why not always use SYMMETRIC?
It can mask reversed-bound bugs that an empty result would have exposed.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.