Skip to content

G-3180: Always specify column names instead of positional references in ORDER BY clauses.

Major

Changeability, Reliability

Reason

If you change your select list afterwards the order by will still work but order your rows differently, when not changing the positional number. Furthermore, it is not comfortable to the readers of the code, if they have to count the columns in the select list to know the way the result is ordered.

Example (bad)

1
2
3
4
5
6
select upper(first_name)
      ,last_name
      ,salary
      ,hire_date
  from employees
 order by 4,1,3;

Example (good)

1
2
3
4
5
6
7
8
select upper(first_name) as first_name
      ,last_name
      ,salary
      ,hire_date
  from employees
 order by hire_date
      ,first_name
      ,salary;