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;
Sort by ordinal positions of columns But instead of specifying the column names explicitly, it uses the ordinal positions of the columns: SELECT first_name, last_name FROM sales. customers ORDER BY 1, 2; In this example, 1 means the first_name column and 2 means the last_name column.
Employee] Order by 3 DESC. In this query, column birthdate is at the 3rd position; therefore, we can use three in the Order by clause to sort results on this column data. Note: I would not recommend using column position in Order By clause. You should always use a column name in Order by clause.
You also may notice that the number 4 is specified in the order by clause. The number 4 specifies the position of the columns in the SQL query. In this case, position of BusinessEntityID is 1, FirstName is 2, MiddleName is 3 and LastName is 4. 1.
It means to group by the first column of your result set regardless of what it's called. You can do the same with ORDER BY .
order by 1
means "order by the first field I selected" -- i.e., in this case, the same as order by playerno
, because playerno
was the first field in the list.
In case you want the official wording, here's what the SQL-92 standard1 says:
10)If ORDER BY is specified, then each <sort specification> in the
<order by clause> shall identify a column of T.
Case:
a) If a <sort specification> contains a <column name>, then T
shall contain exactly one column with that <column name> and
the <sort specification> identifies that column.
b) If a <sort specification> contains an <unsigned integer>,
then the <unsigned integer> shall be greater than 0 and not
greater than the degree of T. The <sort specification> iden-
tifies the column of T with the ordinal position specified by
the <unsigned integer>.
In this case, b
is 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).
This is known as "ORDER BY ordinal", basically order by the column in that position. Order by 1 means order by the first selected column. In your example, it would be the equivalent of saying ORDER BY playerno
I wouldn't recommend doing it this way though as it's not clear what column it's referencing and if the column order changes the query will return different results.
More resources:
Quick Tip: Order By 1 Desc
Bad habits to kick : ORDER BY ordinal
SQL: order by
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With