开发者

Where to put WHERE clause?

开发者 https://www.devze.com 2023-03-10 16:32 出处:网络
Why can\'t I put WHERE clause after ORDER BY? SELECT column1 FROM myTable ORDER BY column2 WHERE column2 = 5

Why can't I put WHERE clause after ORDER BY?

SELECT column1
FROM myTable
ORDER BY column2
WHERE column2 = 5

Is there any standard reference? Where can i get a SQL stan开发者_开发技巧dard draft?


Swap the last two lines:

SELECT column1
FROM myTable
WHERE column2 = 5
ORDER BY column2

Your SELECT ... FROM ... WHERE statement is the data you extract, and the ORDER BY sorts it.

But sorting that query would be pointless, as you already know that column2 is equal to 5.


The WHERE clause should go before the ORDER BY clause. Here's a good article showing the format of a SELECT statement in T-SQL.


The details of the SELECT syntax are here: http://msdn.microsoft.com/en-us/library/ms189499.aspx


That's because SQL has a syntax order, same happens with functions on most languages, what will happen then if you switch the params? you won't get the expected results...

Here's a quick guide reference for the basic SQL Statements and the order of Syntax: http://www.1keydata.com/sql/sql-syntax.html


The Standards are discussed near the end of this document http://en.wikipedia.org/wiki/SQL though you may have to pay for a copy of the official standards document from ISO.

0

精彩评论

暂无评论...
验证码 换一张
取 消