[ACCEPTED]-How do you efficiently determine if a Postgres table has rows-postgresql

Accepted answer
Score: 15

select true from table limit 1;


Score: 6
select exists(select * from your_table_here) as has_row


Score: 2

Try this:

SELECT t.primary_key IS NOT NULL FROM table t LIMIT 1;

You will get TRUE if there are 1 records and NULL if there are none.

Score: 1

If all you care about is 1 row or no rows. Limit 5 your query to the first row - why count 4 all of the rows just to find out if there's 3 1 or more, or zero...

use the equivalent 2 of ROWNUM = 1 or TOP 1 or whatever postgres 1 gives you.

Score: 0

How a count on the primary key field where 3 it is NOT NULL, limiting the query at 1 2 response?

Since a primary key must exist, if 1 there is one, you have data, yes?

More Related questions