Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1.3 Truncating Relations

Last updated: August 27, 2024

Sometimes you may want just a portion of rows generated by a query.

LIMIT and OFFSET

Sometimes we want to limit the result to a few tuples via LIMIT <val>. For example, say we want to order Stops by descending age, return top 15:

SELECT *
FROM Stops
ORDER BY age DESC LIMIT 15;

Sometimes we want to start the output at a particular point via OFFSET <val>. For example, say we want to order Stops by descending age, return positions 11 to 15:

SELECT *
FROM Stops
ORDER BY age DESC
LIMIT 5
OFFSET 10;

Sampling

We can also sample tuples, or select a subset of tuples. This is desirable when the original dataset might be too large, and you want to experiment quickly before running operations on the full dataset. There are 3 sampling methods discussed in this class:

RANDOM is expensive for large tables, because we must first compute all resulting rows, then sort all the rows before returning a truncated subset. While BERNOULLI is faster than RANDOM, it is slower than SYSTEM, due to more random accesses. SYSTEM is therefore the fastest method, but the least “random” due to page-level sampling.