Select count(*) /*from*/ table - Mailing list pgsql-general

From Alexander Shulgin
Subject Select count(*) /*from*/ table
Date
Msg-id CAM-UEKTeLuDaYYfTh0XvL+xhduwsyfO=bmEa4s1n=jsi5XLSXg@mail.gmail.com
Whole thread Raw
Responses Re: Select count(*) /*from*/ table
List pgsql-general
Hello,

Today I've mistyped a SELECT (effectively omitting the FROM clause):

$ SELECT COUNT(*) my_table;
 my_table
----------
        1
(1 row)

Apparently, my_table was treated as an alias to the COUNT(*)
expression.  This has been discussed before, e.g. here:
http://archives.postgresql.org/pgsql-general/2011-03/msg00331.php

OK, but why it gives 1, and not 0?

A hint might be that if you try "SELECT 1+1, COUNT(*)" or any other
SELECT query w/o the FROM list, the result set always(?) consists of
exactly 1 row.

But does it really make sense?  You cannot run a "SELECT *" w/o the
FROM list, and why "COUNT(*)" on empty FROM list would return a value
greater than on e.g. FROM empty_table?

I understand that there's really not much point in running COUNT w/o
the FROM list, but maybe we should just disallow COUNT(*) with empty
FROM list?  It will also save from interpreting the result "1" as the
number of rows in the "table" when you mistakenly forgot to add FROM.

--
Regards,
Alex

pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Dump large DB and restore it after all.
Next
From: Condor
Date:
Subject: Re: Dump large DB and restore it after all.