I often see people answer MySQL questions with queries like this:
SELECT DAY(date), other columns
GROUP BY DAY(date);
SELECT somecolumn, COUNT(*)
HAVING COUNT(*) > 1;
SELECT DAY(date) AS day, other columns
GROUP BY day;
SELECT somecolumn, COUNT(*) AS c
HAVING c > 1;
I think this can be tested using sleep() function,
for example take a look at this demo: http://sqlfiddle.com/#!2/0bc1b/1
Select * FROM t; | X | |---| | 1 | | 2 | | 2 | SELECT x+sleep(1) FROM t GROUP BY x+sleep(1); SELECT x+sleep(1) As name FROM t GROUP BY name;
Execution times of both queries are about 3000 ms ( 3 seconds ).
There are 3 records in the table, and for each record the query sleeps for 1 second only,
so it means that the expression is evaluated only once for each record, not twice.