Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 why select count(*) returns 1?

Author  Topic 

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-05-31 : 00:41:45
Why "SELECT COUNT(*)" returns 1.


what is the means?


Thanks
js.reddy

nirangava
Starting Member

4 Posts

Posted - 2011-05-31 : 00:51:49
if u run the querry like this it shuld get the row count of that table. if it's return 1 it means this table contains only one row.
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-05-31 : 00:55:45
Hi nirangava, thank you for your response.

But my query look like:

SELECT COUNT(*)

i didn't specify table name.

Go to Top of Page

nirangava
Starting Member

4 Posts

Posted - 2011-05-31 : 02:59:55
oho... got the point.
Go to Top of Page

Bazalddo
Starting Member

22 Posts

Posted - 2011-05-31 : 07:00:58
Hi js.reddy,

In this case the count function counts whatever is bewteen the brackets, you could specify COUNT('Apple') and it will return 1. This is because there is one word in the box, just like if you have a asterisk in the box. It makes no difference.

Hope this helps

Baz

Shahbaz Ahdi
Apps Developer
ODEON/UCI Cinema
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-31 : 09:06:27
Also read the comments
http://blog.sqlauthority.com/2011/05/30/sql-server-puzzle-select-vs-select-count/

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

lappin
Posting Yak Master

182 Posts

Posted - 2011-06-01 : 11:10:06
Slightly off topic but
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('MyTable') AND indid < 2
gives same result as select count(*) from MyTable without doing a table scan
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-06-01 : 17:08:53
quote:
Originally posted by lappin

Slightly off topic but
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('MyTable') AND indid < 2
gives same result as select count(*) from MyTable without doing a table scan

Not always. If you need a truly accurate count you need to go to the table directly. System metadata objects are not 100% accurate.
Go to Top of Page

lappin
Posting Yak Master

182 Posts

Posted - 2011-06-02 : 06:47:58
I've read a lot of opinions on this but I haven't seen anything definitive. Some people say it is dependent on statistics being up to date - but is this documented?
rows in sys.sysindexes is defined as "Data-level row count"
http://technet.microsoft.com/en-us/library/ms190283(SQL.110).aspx
Go to Top of Page
   

- Advertisement -