November 21, 2011 at 8:33 pm
It will return the same if the table has data.
If the table is empty, sum(1) will return Null where count(*) would return 0, so not always exactly the same.
November 21, 2011 at 9:53 pm
Explanation
The sum(1) returns exactly the same as count(*).
This is incorrect as Jostein already explained.
Explanation
Both have the same execution plan as well.
This is incorrect too. Execute the following script and you will see the difference.
SET SHOWPLAN_TEXT ON;
GO
SELECT SUM(1) FROM TestDatabase.dbo.TestTable;
SELECT COUNT(*) FROM TestDatabase.dbo.TestTable;
GO
SET SHOWPLAN_TEXT OFF;
GO
Here is the first execution plan.
|--Compute Scalar(DEFINE: ([Expr1004]=CASE WHEN [Expr1005]=(0) THEN NULL ELSE [Expr1006] END))
|--Stream Aggregate(DEFINE: ([Expr1005]=Count(*), [Expr1006]=SUM((1))))
|--Table Scan(OBJECT: ([TestDatabase].[dbo].[TestTable]))
Here is the second execution plan.
|--Compute Scalar(DEFINE: ([Expr1004]=CONVERT_IMPLICIT(int,[Expr1005],0)))
|--Stream Aggregate(DEFINE: ([Expr1005]=Count(*)))
|--Table Scan(OBJECT: ([TestDatabase].[dbo].[TestTable]))
So the first execution plan can be written using the following pseudocode:
if count(*) == 0
QueryResult = NULL
else
QueryResult = sum(1)
While the second execution plan does not contain such condition:
QueryResult = count(*)
November 21, 2011 at 11:52 pm
Aside from the small inconsistencies mentioned earlier, a fine question. A bit too easy though 😀
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 22, 2011 at 2:38 am
DECLARE @t TABLE (id INT)
SELECT SUM(1) AS sum_, COUNT(*) AS cnt_ FROM @t
sum_=NULL
cnt_=0
November 22, 2011 at 3:19 am
easy question! but you must have careful with the affirmations!!!!
November 22, 2011 at 6:17 am
This is a good question, provided the caveats mentioned previously.
Thanks,
Matt
November 22, 2011 at 6:21 am
Thanks for the question.
Another answer that may have fooled a few could have been: "1 row with the sum of all the values from the 1st column of every record in the table."
November 22, 2011 at 7:10 am
Nice question, but only works for non-empty tables.
The reason it doesn't work for empty tables is an excellent illustration of SQL's idiotic confustion between emptiness and nullity - only a committee of mathematical illiterates could have decided that teh sum of the empty set should not be zero.
Tom
November 22, 2011 at 7:12 am
I needed this simple question this morning, ignoring the basic issues mention already as can be seen in the answer selection.
November 22, 2011 at 7:24 am
Hmmm. Interesting. Thanks.
November 22, 2011 at 8:59 am
Nice first 15 seconds of my workday. 🙂
Thanks!
November 22, 2011 at 9:00 am
tks for the question
November 22, 2011 at 9:34 am
Nice and easy question. Thanks!
November 22, 2011 at 2:19 pm
Sorry, but I don't see where the reference given supports the explaination.
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply