In your second example you're using a correlated subquery. You could use a non-correlated subquery (derived table) instead. This will be faster if you happen to list all the foos, because a correlated subquery would have to be called once for each row of the outer query.
This is an example using a derived table:
SELECT foo.*
FROM foo
JOIN (
SELECT MAX(Y) max_time, X
FROM foo
GROUP BY X
) d_foo ON (d_foo.X = foo.X AND
d_foo.max_time = foo.Y);
Test case:
INSERT INTO foo VALUES (1, '2010-01-01 12:00:00', '1');
INSERT INTO foo VALUES (1, '2010-01-03 12:00:00', '2');
INSERT INTO foo VALUES (2, '2010-01-05 12:00:00', '3');
INSERT INTO foo VALUES (2, '2010-01-02 12:00:00', '4');
INSERT INTO foo VALUES (3, '2010-01-08 12:00:00', '5');
INSERT INTO foo VALUES (4, '2010-01-03 12:00:00', '6');
INSERT INTO foo VALUES (4, '2010-01-04 12:00:00', '7');
Result:
+---+---------------------+------+
| X | Y | Z |
+---+---------------------+------+
| 1 | 2010-01-03 12:00:00 | 2 |
| 2 | 2010-01-05 12:00:00 | 3 |
| 3 | 2010-01-08 12:00:00 | 5 |
| 4 | 2010-01-04 12:00:00 | 7 |
+---+---------------------+------+
4 rows in set (0.02 sec)
However, if you will always be restricting your result to just one X
, your solutions are probably fine. Check out @Mark Byers' answer for further tips on this.