Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

This is something that bothered me a lot at school.

5 years ago, when I learned SQL, I always wondered why we specify first the fields we want and then where we want them from.

According to my idea, we should write:

From Employee e
Select e.Name

So why the norm says:

Select e.Name -- Eeeeek, what e means ?
From Employee e -- Ok, now I know what e is

It took me weeks to understand SQL, and I know that a lot of that time was consumed by the wrong order of elements.

It is like writing in C#:

string name = employee.Name;
var employee = this.GetEmployee();

So, I assume that it has a historical reason, anyone knows why?

share|improve this question

closed as not constructive by Thomas Owens Mar 21 '12 at 14:07

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

It's a DBA conspiracy to keep OO monkeys in their place. – gbn Dec 30 '11 at 13:43
Unfortunately, I can't find any relevant info in the paper that introduced SEQUEL, and I don't think there are specific citations that answer your question. gnat's answer is possibly the best explanation though - but I wouldn't dismiss the conspiracy theory. – Yannis Dec 30 '11 at 14:07
Personally, I've always wished Linq could not have used the standardized SQL syntax. – jp2code Dec 30 '11 at 15:53
The clauses in a SELECT statement are not the order of the operation. – S.Lott Dec 30 '11 at 17:06
Nice question. You're next one should be why INSERT and UPDATE queries had to use different syntax models: (field1, field2) VALUES (f1, f2) vs (field1=f1, field2=f2). – LarsTech Dec 30 '11 at 18:10

6 Answers 6

up vote 74 down vote accepted

Originally SQL language was called SEQUEL standing for

  • Structured English Query Language
    with the emphasize on English, assuming it to be close in spelling to natural language.

Now, spell these two statements as you'd spell English sentences:

  1. "From Employee table e Select column e.Name"
  2. "Select column e.Name From Employee table e"

Second sounds closer to natural English language that's why it is set as norm.

BTW same reasoning goes to Where etc - SQL statements were intentionally designed to sound close to natural language.

share|improve this answer
Of course Microsoft ignored that with LINQ as the FROM comes first! – Scott Sellers Dec 30 '11 at 13:46
There's a lot of lookahead logic in English :/ – Michael K Dec 30 '11 at 13:47
@Digger - that was by design: they couldn't support intellisense in the select part if the select came first. – Scott Whitlock Dec 30 '11 at 13:49
@Digger: LINQ follows the OO/modern Object.Method or Object.Property. Don't forget SQL has been around for 40 years – gbn Dec 30 '11 at 13:49
I should have post this question on instead :) – Cyril Gandon Dec 30 '11 at 13:57

Because SELECT is required in a select statement and FROM is not.

Select 'This String'

Of course your sql statement can be parsed to look for the SELECT, DELETE, UPDATE after the FROM, but is is really that big of a deal?

Remember, this was all done before intellisense. It's not that complicated.

share|improve this answer
Though, you could also write FROM myTable; instead of FROM myTable SELECT *; This only seems like a requirement because it's what your used to. – user606723 Dec 30 '11 at 17:10
Just because it's required doesn't mean it has to come first. – LarsTech Dec 30 '11 at 18:07
In ANSI SQL FROM is required. This is why many RDBMSs have a table called DUAL or other single row dummy table – Martin Smith Dec 30 '11 at 18:20
@LarsTech - it doesn't have to come first, but why make it complicated. It's called a select statement, just start with the word select. – JeffO Dec 30 '11 at 18:49
@JeffO: Okay. SELECT FROM Customers COLUMNS FirstName, LastName, PhoneNumber. – Allon Guralnek Jan 5 '12 at 19:22

Do not know an answer that I could back up by references, but if I had to speculate: SQL is a declarative language, a statement of such language describes what you would like to do as opposed to how you would like to do it.

As such, "SELECT X FROM Y" sounds as a more appropriate way of answering "What would I like to select from the database", as opposed to writing "FROM Y SELECT X".

In addition, in SQL, the SELECT/UPDATE/INSERT specifies the type of operation you are about to do and FROM is just a clause that helps you select from the right table in the database. Again, what are you doing with data takes precedence over how exactly you are going to achieve that.

share|improve this answer
+1: It's not imperative or procedural. The order of the clauses is merely a fit with English. Nothing more. – S.Lott Dec 30 '11 at 17:06
ON and WHERE may be better examples of the importance of order of clauses in a select statement. – JeffO Dec 30 '11 at 22:50

SQL is a structured query language targeted to English speakers. SELECT, INSERT, UPDATE, and DELETE are imperative commands. In English imperative commands begin the sentence or statement. Compare:

West young man go!


Go west young man!

SQL follows the second (imperative) format. Also the four imperative commands have three significantly different formats. Consider:

FROM    employees a,
        accounts b


INTO    customers a

If you know the action you are undertaking, it is easier to select the correct format.

In the case of select, you determine which attributes you want, then add the tables that have them. As you build the selection criteria, you may add additional tables. When you are dynamically adding criteria, it can usually be done at the end of a static portion of the query.

share|improve this answer
So Yoda was not involved in developing SQL. – adam f Jan 1 '12 at 22:40
Interesting that you bring up UPDATE. UPDATE ... FROM is not a English-like structure, IMO. Not that I have any better suggestions... – Rob Jan 5 '12 at 1:03
The intent was to point out that the preposition needs to match the verb. – BillThor Jan 5 '12 at 1:19

I think it would significantly complicate parsing, especially with subqueries, e.g.

  FROM Foo f
  JOIN (FROM Bar b
        WHERE b.ID = f.ID
        UPDATE b
           SET b.Wibble = 1) x
    ON x.ID = f.ID

Parsing this would be more complicated. You could not tell that the UPDATE was a syntax error until you had parsed the FROM clause, and the parser would have to remember enough context to know that it was parsing a subquery. I don't think updates are permissible in subqueries anyway, but if they were (maybe with a RETURNING clause) then you might not be able to tell this was invalid until you had parsed the SELECT statement.

This would at least increase k (lookahead) for the grammar and at worst make it context sensitive, although this is stretching the bounds of my rather dimly remembered compiler design papers from university.

share|improve this answer
Looking at the Wiki article for QUEL that was developed at around the same time the order of the syntax clauses is what the OP suggests. – Martin Smith Dec 30 '11 at 19:09

SQL statements begin with verbs. That was the choice of the language designers, and many programming languages work that way. Semantically, it is not uncommon to see programming languages that work like this:

verb(noun, noun, noun);

Also, in the case of SELECT statement that you give as an example, your proposed syntax would put the object first in the statement. Instead of a VSO (verb, subject, object) sentence order, you would have OVS, which would be a very strange when compared to natural languages. SVO (e.g. English), VSO (e.g. Arabic), and SOV (e.g. Latin) are more reasonable approximations of human speech.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.