Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ORDER BY 5 DESC mean?

SELECT Departamentos.Nome_Dep,  
       Funcionarios.Nome AS Funcionario,
       Funcionarios.Salario,
       AVG(Funcionarios.Salario) OVER(PARTITION BY Departamentos.Nome_Dep) "Média por Departamento"
       Salario - AVG(Funcionarios.Salario) OVER(PARTITION BY Departamentos.Nome_Dep) "Diferença de Salário"   FROM Funcionarios
INNER JOIN Departamentos
    ON Funcionarios.ID_Dep = Departamentos.ID
ORDER BY 5 DESC

The Order By 5 is throwing me off. I've never anything like it. Order By [colunmname] yes, but Order By [number], never seen before. I pulled this off an article.

Note: This is T-SQL.

Source: Window Functions in SQL Server 2005, 2008, 2012

like image 878
dotnetN00b Avatar asked Nov 02 '11 17:11

dotnetN00b


People also ask

What does ORDER BY 2 desc means in SQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

What does ORDER BY 1 desc mean?

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

What is meant by ORDER BY 2?

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.

Can we use ORDER BY 2 desc in SQL?

After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.


2 Answers

This will order by the 5th field in this SELECT statement

like image 174
Cory Avatar answered Oct 18 '22 23:10

Cory


Order by the 5th column in the result set.

like image 38
Jonathan Allen Avatar answered Oct 18 '22 22:10

Jonathan Allen