I had a question in MySQL, did it correctly. But the book code differs a little.
Book:
use tennis;
select playerno, datediff(coalesce(end_date, current_date),
begin_date) as Difference, position
from committee_members
where datediff(coalesce(end_date, current_date), begin_date) > 500
order by 1;
What is this order by 1 ?
My code also works and is almost the same except:
select playerno, datediff(coalesce(end_date, current_date) AS Data,
order by Data;
order by 1means "order by the first field I selected" — i.e., in this case, the same asorder by playerno, becauseplayernowas the first field in the list.In case you want the official wording, here’s what the SQL-92 standard1 says:
In this case,
bis the one that seems to apply.More recent versions of the SQL standard have removed this capability though, so new code should generally avoid it. SQL-based database servers have been deprecating it for a while now, but most continue to support it for the sake of backward compatibility. At the same time, the fact that they’ve deprecated it indicates they no longer consider it a feature they really need to support, so it could be removed at any time with no further warning (e.g., if they find a bug in that part of their code, they might decide the best way to fix the bug is to just disable that feature).
1. This quote is from a freely-available draft rather than the approved standard. While I’m sure there are at least a few changes between this draft and the final text of the standard (not to mention between one version of the standard and another) it seems unlikely that something this fundamental would change between the draft and the final standard.