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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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