sql order by select 1 code example

Example: order by 1

SELECT DISTINCT department 
FROM employees 
ORDER BY 1 

 
ORDER BY 1 ...is known as an "Ordinal" - the number stands for the 
column based on the number of columns defined in the SELECT clause.  

it simply means sorting the view or table by 1st column of 
query's result. 

------------------------------------------------ 

SELECT department, hire_date 
FROM employees 
ORDER BY 2 

It would order by hire_date since it's the second  
column in the results.  


THIS IS NOT A RECOMMENDED PRACTICE AS THE COLUMNS CAN CHANGE LATER! 
This can cause a lot of confusion if multiple people work on the
code base.  
It's better to assign an alias to each column name rather 
than using a column number.

Tags:

Misc Example