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?
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?
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 *
.
if no record is matched the count will return 0. (so yes, count always returns a result, unless you have some syntax error)
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