Sql query result if no records found
what will be the output o开发者_Go百科f the below given query, if no matching records are found
select avg(salary) from employee where dept='sales'
Will it return 0 or null ?
It returns null: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_avg
Have a look at this.
It returns NULL
Easy test in SQL Server:
DECLARE @T TABLE (id int)
INSERT INTO @T
SELECT NULL
SELECT AVG(id) FROM @T
Returns NULL
By default in MySQL and i'm pretty sure it is too in MSSQL, all aggregation functions return NULL if no data or if ANY OF THE DATA IS NULL, you cannot compute an aggregation on missing data.
精彩评论