简体   繁体   中英

Does COUNT(*) always return a result?

If I run a query such as:

SELECT COUNT(*) as num FROM table WHERE x = 'y'

Will it always return a result, even when the query doesn't match any record? Or do I need to validate and make sure a row is returned as the result?

Yes, because it's an aggregate and returns zero. Unless you add GROUP BY in which case no result because there is no group...

MAX/SUM etc would return NULL unless you add GROUP BY then no rows. Only COUNT returns a number for no results

Edit, a bit late: SUM would return NULL like MAX

Edit, May 2013: this applies to all main RDBMS. I guess as per the ANSI standard

Yes, the return value of the "count" function itself is always a non-null integer, without exception.

Having said that, you may be more interested in the return value of your query , which is actually a result set . If that's the case, then you simply have to consider whether your query includes a "group by" clause.

A non-grouped query with aggregate functions like

select count(*), sum(*), max(*), min(*) from table

produces a result set with a single record containing the return value(s) of the aggregate function(s). In this case, your result set will always have exactly one record, with columns containing the return values of whatever aggregate functions you included. Within this lone record, the return value of count will always be a non-null integer, while the return values of the other aggregate functions such as min, max, and sum may be null. In the above example, your returned record may be "zero, null, null, null", but will never be "null,null,null,null", since count never returns null. So if you're calling only "count", then that one column in that one record returned will always contain the non-null return value of count; hence you can rest assured that the scalar return value of your count query will always be a non-null integer.

On the other hand, a grouped query with aggregate functions like

select count(*), sum(*), max(*), min(*) from table group by column

produces a result set with zero or more records where each record contains the return value of the aggregate function(s) for each group identified. In this case, if there are no groups identified, there are no groups for the aggregate functions to run on, the functions are never run, and the "query return value" will be an empty set with no records at all.

Test it out on a table with no records to see the results:

create table #t (c int);
select COUNT(c), MIN(c), MAX(c), SUM(c) from #t; --returns one record {0, null, null, null}
select COUNT(c), MIN(c), MAX(c), SUM(c) from #t group by c; --returns no records
drop table #t;

So whether you're calling count, min, max, or sum, it is important to understand not only the return values of individual aggregate functions, but also the "return value" of the query itself, and what that result set contains.

聚合函数 Count() 总是返回值

是的,在这种情况下它会返回 0。

There will always be a row of result like this:

| COUNT(*) |
------------
| 0        |

if there are no matches.

By the way, I would prefer to count only the primary key instead of * .

如果没有匹配的记录,count 将返回 0。(所以是的,count 总是返回一个结果,除非你有一些语法错误)

是的,它总是会返回一个数字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM