select count(*)

Lists: pgsql-general
From: Rajesh Kumar Mallah <mallah(dot)rajesh(at)gmail(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: select count(*)
Date: 2011-03-09 17:44:42
Message-ID: AANLkTikijnqxh1zLJHQSqh6Ww82Y068PQ0Du9QO2LUmM@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Dear List ,

if we simply do select count(*) and not specify any table then it gives 1
eg:

bric=# SELECT count(*) from job ;
count
-------
2380
(1 row)

bric=# SELECT count(*) job ;
job
-----
1
(1 row)

bric=# SELECT count(*) ;
count
-------
1
(1 row)

bric=# SELECT count(*) job_non_exist ;
job_non_exist
---------------
1
(1 row)

bric=# SELECT count(*) jo1b ;
jo1b
------
1
(1 row)

bric=# SELECT count(*) none ;
ERROR: syntax error at or near "none"
LINE 1: SELECT count(*) none ;

I fail to see any progression ?

regds
mallah.


From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Rajesh Kumar Mallah <mallah(dot)rajesh(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: select count(*)
Date: 2011-03-09 17:50:32
Message-ID: 20110309125032.20e0f25c.wmoran@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In response to Rajesh Kumar Mallah <mallah(dot)rajesh(at)gmail(dot)com>:

> Dear List ,
>
> if we simply do select count(*) and not specify any table then it gives 1
> eg:
>
> bric=# SELECT count(*) from job ;
> count
> -------
> 2380
> (1 row)
>
> bric=# SELECT count(*) job ;
> job
> -----
> 1
> (1 row)
>
>
>
> bric=# SELECT count(*) ;
> count
> -------
> 1
> (1 row)
>
>
>
> bric=# SELECT count(*) job_non_exist ;
> job_non_exist
> ---------------
> 1
> (1 row)
>
> bric=# SELECT count(*) jo1b ;
> jo1b
> ------
> 1
> (1 row)
>
> bric=# SELECT count(*) none ;
> ERROR: syntax error at or near "none"
> LINE 1: SELECT count(*) none ;
>
>
> I fail to see any progression ?

When you don't specify a FROM clause, you get 1 because it's
returning 1 row. No matter what you alias the result to, it's not going
to change the result, unless of course you try to alias it to an SQL
reserved word, such as "none", without quoting it. Of course, if you
include the optional AS, it probably makes more sense what's going on:

SELECT count(*) AS jolb;
SELECT count(*) AS none;
SELECT count(*) AS "none";

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/


From: Rajesh Kumar Mallah <mallah(dot)rajesh(at)gmail(dot)com>
To: Bill Moran <wmoran(at)potentialtech(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: select count(*)
Date: 2011-03-09 19:09:43
Message-ID: AANLkTinKwwnLwv+wwESb9byz8DE70KPjw3xLiRJ0E+4A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

i "discovered" it as a result of typo :)

we usually select expressions without tables
eg select 1+2 ; etc and the results are as expected,
somehow i failed to stretch the analogy to count(*)
which is mostly used over tables or table expression.

thanks anyways.

regds
mallah.

On Wed, Mar 9, 2011 at 11:20 PM, Bill Moran <wmoran(at)potentialtech(dot)com>wrote:

> In response to Rajesh Kumar Mallah <mallah(dot)rajesh(at)gmail(dot)com>:
>
> > Dear List ,
> >
> > if we simply do select count(*) and not specify any table then it gives 1
> > eg:
> >
> > bric=# SELECT count(*) from job ;
> > count
> > -------
> > 2380
> > (1 row)
> >
> > bric=# SELECT count(*) job ;
> > job
> > -----
> > 1
> > (1 row)
> >
> >
> >
> > bric=# SELECT count(*) ;
> > count
> > -------
> > 1
> > (1 row)
> >
> >
> >
> > bric=# SELECT count(*) job_non_exist ;
> > job_non_exist
> > ---------------
> > 1
> > (1 row)
> >
> > bric=# SELECT count(*) jo1b ;
> > jo1b
> > ------
> > 1
> > (1 row)
> >
> > bric=# SELECT count(*) none ;
> > ERROR: syntax error at or near "none"
> > LINE 1: SELECT count(*) none ;
> >
> >
> > I fail to see any progression ?
>
> When you don't specify a FROM clause, you get 1 because it's
> returning 1 row. No matter what you alias the result to, it's not going
> to change the result, unless of course you try to alias it to an SQL
> reserved word, such as "none", without quoting it. Of course, if you
> include the optional AS, it probably makes more sense what's going on:
>
> SELECT count(*) AS jolb;
> SELECT count(*) AS none;
> SELECT count(*) AS "none";
>
> --
> Bill Moran
> http://www.potentialtech.com
> http://people.collaborativefusion.com/~wmoran/
>