What is the purpose of Order By 1 in SQL select statement?

Viewed 228790

I'm reading through some old code at work, and have noticed that there are several views with an order by 1 clause. What does this accomplish?

Example:

Create view v_payment_summary AS
SELECT A.PAYMENT_DATE,
       (SELECT SUM(paymentamount)
          FROM payment B
         WHERE PAYMENT_DATE = B.PAYMENT_DATE
           and SOME CONDITION) AS SUM_X,
       (SELECT SUM(paymentamount)
          FROM payment B
         WHERE PAYMENT_DATE = B.PAYMENT_DATE
           and SOME OTHER CONDITION) AS SUM_Y    
FROM payment A    
ORDER BY 1;
9 Answers

ORDER BY 1 means order by 1st column of the result set

SELECT case when b.salary is null then a.employee_id else b.employee_id end employee_id FROM Employees a FULL JOIN Salaries b on b. employee_id = a.employee_id WHERE b.salary is null or a.employee_id is null order by 1

it simply means sorting the view or table by 1st column of the query's result. above this query, the 'employee_id' column is the first column but this column is selected from two tables that's why to use this type of sorting.

Related