compare
SELECT distinct u_id,timestamp as time
FROM my_table;
and
SELECT distinct u_id,max(timestamp) as time
FROM my_table;
When my table has no rows at all (or if I add a where clause that matches no rows):
The former returns an empty results set (which is what I expect)
while the later returns a single row that has null as the value for both its fields.
Can someone please explain to me why does the second one acts as it does?
MySQL documentation says
And if you have no data then it just returns both values as NULL.
If you want the second query return the empty resultset too, then you must filter out the NULL values for example with HAVING clause that you can use with aggregate functions: