Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:33+00:00 2026-05-26T23:20:33+00:00

I use Postgresql 9.1. I have a table and I would like to get

  • 0

I use Postgresql 9.1. I have a table and I would like to get two things done in one query:

First: select one random sample row:

select * from table order by random() limit 1;

Second: count the number of rows the sample was selected from:

select count(1) from table;

Since a proper random() function has to know about all rows, I wonder if there is a way to get both results in one query.

I am guessing for something link:

select count(1), first.* from table order by random();

but i don’t really know how to do it.

So I want a random row and the total number of rows.

Thanks for any ideas.

Steve

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:20:34+00:00Added an answer on May 26, 2026 at 11:20 pm

    One way to do this:

    SELECT *, (SELECT count(*) FROM tbl) AS ct
    FROM   tbl
    ORDER  BY random()
    LIMIT  1;
    

    If the table is huge and performance is an issue, there are tricks to speed up the random selection. Having a serial column without gaps would help a lot with that …

    If that is for you, have a look at this related answer: Random Row in MySQL
    Works in PostgreSQL, too. Only substitute random() for rand().

    BTW, the random() function works absolutely independent of the number of rows.


    Here is a variant that produces the same with only one sequential scan:

    SELECT *, count(*) OVER () AS ct
    FROM   ef.adr
    ORDER  BY random()
    LIMIT  1;
    

    A quick test with a table of 5k rows was slower, though.
    Another one with a table of 400k rows shows a similar result. slightly slower. Window functions can be performance hogs. And count(*) is known to be rather slow for large tables in PostgreSQL, because every tuple has to be visited.

    Index-only scans (“covering indexes”) has recently been implemented in the devel version of 9.2. So we are in for a speedup with the next version here. More about that in a related post on dba.stackexchange.com:

    In the meantime, 230 ms for the 400k table is not too bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like the following: SELECT * FROM table WHERE
say I have a postgresql table with the following values: id | value ----------
Edit: In short what strategy should one use on insert and select scripts with
I have a 'users' table with two columns, 'email' and 'new_email'. I need: A
I have a database with one of the table storing articles. For every article
I have a couple of million rows in a postgresql table. I have up
I would like a fairly efficient way to condense an entire table to a
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
I'm writing an MFC App to automatically configure Postgresql with ODBC for use by
When I'm in Windows, I use the excellent MicroOLAP Database Designer for PostgreSQL, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.