Live data from Hacker News

Shouldn't FROM come before SELECT in SQL? (2011)

stackoverflow.com

81–90 of 137 posts

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#81
post #37

FROM is not a command, it's a parameter, and an optional one at that. This is valid SQL. SELECT 1; The SQL commands are SELECT, UPDATE, INSERT, etc. Therefore, those commands should be the first thing in an instruction. If you have a file full of SQL, you probably want all the lines to start with those commands. Gonna be pretty weird to read if you have both SELECT and UPDATE lines that start with FROM. Probably diff…

    FROM ℝ SELECT 1
? :-)

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#82
post #19

Azure query language's Kusto, lously based on a mix of F# and SQL, does it, as does LINQ. I end up always starting with the generic form select * from db, and then go from there, as code completion then works if there is at least one db.

You're probably getting downvoted just because of Azure, but you are right. ADX/Kusto and KQL is an extremely powerful query language that answers exactly what OP questions about SQL. Here's an example of a KQL query that I have in my browser... Things | where DeviceTags has "Installed" | order by LastHeardFromTimeStamp desc | take 10 KQL also takes ideas from R's Tidyverse and magrittr package. It takes datasets and…

I don't care, at least on Azure there are real people to talk to, when support is needed. :)

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#83

Earlier quoted context omitted.

> This is valid SQL. > SELECT 1; Something like Oracle's "dual" table would handle this corner case. Or just selecting a value from itself: FROM 1 SELECT 1; or FROM ::integer SELECT 1; Or whathaveyou. In any case, the SELECT 1 case isn't fatal to putting FROM first, merely annoying. > Intelligent tooling can and does already solve this. It can progressively guess as you type, but it can't provide an upfront list of a…

Given how often `SELECT ` is already the developer "default" especially in debugging and REPL work there's a good case that `SELECT ` can truly just be the implied default case and something like `FROM 1` makes perfect sense as a standalone statement.

Even neater.

DuckDB has an implicit "SELECT *" if you just give it "FROM tablename", which fits your example.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#84
I used to work on an Ingres database using QUEL that did have FROM before SELECT, just with different key words. We would use something like:

  RANGE OF t IS table
  RETRIEVE INTO target (t.column1, t.column2) 
  WHERE t.condition
Instead of

  SELECT t.column1, t.column2
  FROM table t
  WHERE t.condition
I still find the joins to be more readable:

  RANGE OF t1 IS table1
  RANGE OF t2 IS table2
  RETRIEVE INTO target (t1.column, t2.column) WHERE t1.common = t2.common

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#85
post #45

Earlier quoted context omitted.

that may very well be the case but perhaps there could be an evolution of the language because for newcomers the idea that the from clause is evaluated/executed/defined first is a pain point. Newbies I help sometimes wonder why things they reference in the SELECT part aren't visible/available in the FROM clause and that's because one is selecting the result of FROM ... JOIN ... WHERE etc anyway

> he idea that the from clause is evaluated/executed/defined first is a pain point Maybe my brain is broken by years of SQL and from learning English as a second language. But isn’t this supposed to follow a fairly mundane English sentence structure? “Select socks and pants from drawer”. If you started saying “from drawer select socks and pants”, wouldn’t that feel like a weird sentence structure to most people?

Both seem equivalent to me. And given how SQL is executed I like the latter approach tbh.

But generally SQL is often very nicely translatable to English at least for me and so long as the SQL isn’t too overly complex.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#86
post #60

I still largely reject the code completion complaint. Specifically, it is very common to know what columns you want before you know what tables have said columns. This is especially true on normalized schemas where you are almost certainly having to do some joins to get all that you want. So, does it somewhat complicate the logic? Of course. It is by no means impossible, though, and unless you are using super generic…

I'm not sure. I think it's fairly common for something like "Now was the column name on this table description_primary or primary_description? I can't remember... Oh well, I'll just SELECT * and figure it out later" to happen. Starting with FROM would at least eliminate that backtracking.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#87
post #37

FROM is not a command, it's a parameter, and an optional one at that. This is valid SQL. SELECT 1; The SQL commands are SELECT, UPDATE, INSERT, etc. Therefore, those commands should be the first thing in an instruction. If you have a file full of SQL, you probably want all the lines to start with those commands. Gonna be pretty weird to read if you have both SELECT and UPDATE lines that start with FROM. Probably diff…

An SQL parser has no problem supporting both forms as there is no parsing ambiguity. (SQL is full of corner cases e.g. EXTRACT(WEEKDAY FROM field) etc. Putting the FROM first, or supporting multiple chained WHERE clauses or allowing WHERE before JOIN and applying to the preceding projection etc is all possible in an SQL parser that chooses to allow some relaxations. Personally, I am really irritated that I can't have…

Nitpick -- SQL was not designed to be used programmatically. Same as shell commands, it's for humans to write manually. Proper API could have way more concise, and much more efficient format to parse. And would avoid a bunch of security issues along the way, same as for shell.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#88
post #3

Linq does this [0], maybe partially because it's SQL-like but built for working with enumerables instead so they could do what they wanted. One other quirk of SQL ordering that always gets me is that SET comes before WHERE in UPDATE. I always get terrified that I'll run without the WHERE or without selecting the WHERE. Thankfully a good database tool like DataGrip will yell at you if you try to do a modifying operati…

I can't remember where I read it, but I believe one of the main reasons why Linq moved the FROM clause to the front was for better intellisense. If you start writing SELECT x in an IDE, there really is no way of providing intellisense for x.

However if you write FROM table SELECT x, the IDE can first provide intellisense for table names while you write your FROM clause. Then it can provide intellisense for the SELECT clause based on columns in the tables/views/etc you listed in your FROM. So it was basically done for better UX.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#89
post #73

SQL is a mess. It's actually a fairly well-thought-out and straightforwards language. However, in the quest to make it 'human-readable', it's very unintuitive to compose.

Then let's stop using it for the wrong purpose. Any DBMS can have two interfaces -- SQL for human interaction, and proper API for machines to talk. More secure and efficient.

Re: Shouldn't FROM come before SELECT in SQL? (2011)

#90
post #37

FROM is not a command, it's a parameter, and an optional one at that. This is valid SQL. SELECT 1; The SQL commands are SELECT, UPDATE, INSERT, etc. Therefore, those commands should be the first thing in an instruction. If you have a file full of SQL, you probably want all the lines to start with those commands. Gonna be pretty weird to read if you have both SELECT and UPDATE lines that start with FROM. Probably diff…

The poster's problem is not recognizing that SQL is not procedural. The SQL query is declarative, though some databases are more literal in turning these into query plans than others.
Post reply on HN