There is no built-in syntax to do that in connection with LIMIT
/ OFFSET
(nor with standard-SQL FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES }]
).
You can achieve your goal with the modulo operator %
:
SELECT *
FROM (
SELECT row_number() OVER () AS rn, ... original SELECT list
FROM ... -- original query
) sub
WHERE rn%10 = 0 -- every 10th row
Since there is no ORDER BY
in the window definition, row numbers are assigned according to the ORDER BY
of the query.
If there is no ORDER BY
at all, you get an arbitrary order of rows. That's still some kind of order, the result is just out of your hands.
You can apply that kind of filter on an individual table with the TABLESAMPLE
syntax.
SELECT * FROM tbl TABLESAMPLE SYSTEM (10); -- roughly 10 %
Or:
SELECT * FROM tbl TABLESAMPLE BERNOULLI (10); -- roughly 10 %
SYSTEM
is faster, BERNOULLI
is more random.
You could even apply the TABLESAMPLE
filter on multiple tables in the same query, like:
SELECT *
FROM tbl1 TABLESAMPLE SYSTEM (10)
JOIN tbl2 TABLESAMPLE BERNOULLI (10) USING (big_id);
But the number of resulting rows can vary wildly. To get a given number of rows, consider the additional module tsm_system_rows
instead. See: