Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

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;

1 Answer

0 votes
by (40.4k points)

ORDER BY 1 

The above syntax is known as an "Ordinal" - Here, the number stands for the column based on the number of columns defined in the SELECT clause. 

In the query that you have provided, it means:

ORDER BY A.PAYMENT_DATE

It's not recommended to use, because it's not explicit/obvious. But, if the column order changes, then the query will be valid. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 17, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...