user653211
user653211

Reputation: 1

Where to put WHERE clause?

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 standard draft?

Upvotes: 0

Views: 2089

Answers (5)

Bueller
Bueller

Reputation: 2344

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.

Upvotes: 1

LuisVM
LuisVM

Reputation: 2793

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

Upvotes: 1

ic3b3rg
ic3b3rg

Reputation: 14927

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

Upvotes: 1

Jacob
Jacob

Reputation: 78910

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.

Upvotes: 1

Blender
Blender

Reputation: 298512

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.

Upvotes: 3

Related Questions