X-Original-To: pgsql-sql@postgresql.org Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 08C5C4762F6 for ; Fri, 7 Mar 2003 01:40:15 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id C5246D621; Thu, 6 Mar 2003 22:40:14 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id BADE15C0A; Thu, 6 Mar 2003 22:40:14 -0800 (PST) Date: Thu, 6 Mar 2003 22:40:14 -0800 (PST) From: Stephan Szabo To: Rajesh Kumar Mallah Cc: Tomasz Myrta , Subject: Re: sql question regarding count(*) In-Reply-To: <200303071201.38192.mallah@trade-india.com> Message-ID: <20030306223558.A87666-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200303/177 X-Sequence-Number: 12217 On Fri, 7 Mar 2003, Rajesh Kumar Mallah wrote: > On Friday 07 March 2003 11:41 am, Stephan Szabo wrote: > > On Fri, 7 Mar 2003, Rajesh Kumar Mallah wrote: > > > Tomasz did you notice the two queries are totally differet on different > > > tables? > > > > A count() on a non-grouped table returns 1 row. > > > > A grouped table returns 1 row per group. If there are no groups, no rows > > are returned. If the groups are filtered by a having clause, it might > > also return no rows. > > True the second query does not return any row. > even in first case no rows are returned. > > where is the difference? > I don't think I understand the question. select userid from users where userid=-1 returns 1 row for each row the where clause returns true and 0 rows if there are none. select count(*) from users where userid=-1 returns 1 row (it's not a grouped table) select count(*) from users where userid=-1 group by userid returns 1 row if there is at least 1 row having userid=-1 or 0 rows otherwise (either there is a group or there isn't) select count(*) from users where userid=-1 group by userid having userid!=-1 should return 0 rows because even if there is a group it'll fail the having clause.