Limit rows with distinct

33 views
Skip to first unread message

livius...@poczta.onet.pl

unread,
Dec 17, 2025, 5:53:22 PM12/17/25
to firebird...@googlegroups.com
Hi

i am curious if

1. 
SELECT
DISTINCT X.NAME
FROM
TABLE_X X
ROWS 2

is equal to

2. 
SELECT
FROM
(
SELECT
DISTINCT X.NAME
FROM
TABLE_X X
) Y
ROWS 2

or maybe it is

3.
SELECT
DISTINCT Y.NAME
FROM
(
SELECT
FROM
TABLE_X X
ROWS 2
) Y

And is it always one of these cases, regardless of the query plan?

I’m fairly sure the answer is “2,” but I need to be certain.

To make the question a bit more complex, consider different LIMIT-style clauses:


a) rows:
SELECT
DISTINCT X.NAME
FROM
TABLE_X X
ROWS 2


b) first:
SELECT
FIRST 2 DISTINCT X.NAME
FROM
TABLE_X X

c)  fetch:
SELECT
DISTINCT X.NAME
FROM
TABLE_X X
FETCH FIRST 2 ROWS ONLY

regards,
Karol Bieniaszewski

Vlad Khorsun

unread,
Jan 6, 2026, 8:53:01 AM (7 days ago) Jan 6
to firebird-support
  Rows limit applied to the final result set, i.e. after sorting and/or grouping.

Regards,
Vlad
Reply all
Reply to author
Forward
0 new messages