Selecting a Random Sample From PostgreSQL

Do you need a random sample of features in a Postgres table? Here is an example of how to select 1,000 random features from a table:

WHERE attribute = 'myValue'
ORDER BY random()
LIMIT 1000;
  1. Ilya said:

    Thank you for this handy hint. I never heard about it before. There is one limitation with this approach in case of variable spatial density of the features. E.g. road network in the downtown of the city has higher density than in suburbs, so such type of a selection will produce biased towards more dense regions subset of the points:

    So, I wonder how to make feature sampling via regular grid or take into account spatial density?

    • underdark said:

      Hi Ilya,

      That’s an interesting question. Maybe you could ask it on There are some really knowledgeable people there.

      Best wishes,

%d bloggers like this: