cockroachdb / cockroachdb/cockroach

sql: support `FETCH .. WITH TIES` clause

Open
#124,982 6 comments 0 reactions 0 assignees View on GitHub
C-enhancement T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

The `WITH TIES` option of `FETCH` can be used to include all rows beyond the limit that tie with the last row in the result set, according to the `ORDER BY` clause (see the [Postgres docs](https://www.postgresql.org/docs/16/sql-select.html#SQL-LIMIT)). Here's an example:

```sql
CREATE TABLE t (k INT PRIMARY KEY, v INT);
-- CREATE TABLE

INSERT INTO t VALUES (1, 100), (2, 20), (3, 100);
-- INSERT 0 3

SELECT * FROM t ORDER BY v FETCH FIRST 2 ROWS WITH TIES;
-- k | v
-- ---+-----
-- 2 | 20
-- 1 | 100
-- 3 | 100
-- (3 rows)
```

Jira issue: CRDB-39183

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.