Home > Article > Backend Development > Using ORDER BY clause in views_PHP Tutorial
Q: Why does SQL Server not allow the ORDER BY clause in view definitions?
Answer: The reason why SQL Server does not allow the use of the ORDER BY clause in the view definition is to comply with the ANSI SQL-92 standard. Because an analysis of the principles of the standard would require a discussion of the underlying structure of Structured Query Language (SQL) and the mathematical theory on which it is based, we cannot fully explain it here. However, if you need to specify the ORDER BY clause in the view, consider using the following method:
USE pubs
GO
CREATE VIEW AuthorsByName
AS
SELECT TOP 100 PERCENT *
FROM authors
ORDER BY au_lname, au_fname
GO
The TOP structure introduced by Microsoft in SQL Server 7.0 is very useful when used in conjunction with the ORDER BY clause. SQL Server supports the use of the ORDER BY clause in views only when used in conjunction with the TOP keyword.
Note: The TOP keyword is SQL Server’s extension to the ANSI SQL-92 standard.