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?
The COUNT(*) function returns a number of rows in a specified table or view that includes the number of duplicates and NULL values.
If you specify the asterisk (*), then this function returns all rows, including duplicates and nulls. COUNT never returns null.
COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.
count(*) means it will count all records i.e each and every cell BUT. count(1) means it will add one pseudo column with value 1 and returns count of all records.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With