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.
Shouldn't FROM come before SELECT in SQL? (2011)
101–110 of 137 posts
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#102Also SELECT should be the last clause (at least for row-oriented DBMS, for columnar databases it might come before WHERE, GROUP BY, ORDER BY to hint which columns we want to select to query on). Row-oriented: FROM table_name WHERE condition GROUP BY ... HAVING ... ORDER BY ... SELECT column1, column2, ...; Columnar: FROM table_name SELECT column1, column2, ... ORDER BY ... WHERE condition GROUP BY ... HAVING ...;
I'm interested to understand why you think it should differ between OLAP and OLTP? Are you suggesting that columns closer to the top matters for OLAP bc OLAP is columnar? Ultimately, a query planner is going to figure out what happens first, so I can't imagine your point has anything to do with execution.
2. I'm not talking about cases where a Query Planner parses the query language DSL, and computes Query Plan out of it. More the case when you kind of have an explicit Query Plan. The best example is FluxQL[1].
--
[1]. https://docs.influxdata.com/influxdb/cloud/reference/syntax/...
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#103Earlier quoted context omitted.
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.
People: stop trying to mangle SQL "because it would be better..."; nah, SQL is supposed to be the "better" already. The idiosyncrasies it has is because it was designed to be kinda-sorta conversational, modeled as an english language query.
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#104Python's syntax also rattles me for the same reason. [item for item in items if item.include] It's almost exactly like a sql statement. The order is confusing. More broadly, foreach loops are also written in the wrong order. More reasonable: foreach(items as item) In the West, we read left to right. Presenting undefined terms before defined ones burdens the mind.
It's a question of ergonomics, just like my favorite chair is not going to be your favorite chair for whatever reason, programming language constructs that bug you doesn't mean it'll bug anyone else.
Your foreach() example bugs the hell out of me, for example. But I'm not campaigning to have it erased from existence, I just don't use languages that model their syntax like that.
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#105Linq 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…
In terms of actually writing queries live, like if you're in the CLI client, the order of UPDATE is definitely terrifying. The workarounds of writing it out of order or as a SELECT first are fine... I'd almost like to see a mode the interactive client sets that just rejects any UPDATE without a WHERE, and you'd have to do WHERE 1 or similar to get an "UPDATE everything."
/* use your PPE */
BEGIN;
/* make the change */
UPDATE foo SET bar = ‘baz’;
/* sanity check */
SELECT * FROM foo WHERE bar != ‘baz’;
/* oops, let’s pretend this never happened */
ROLLBACK;Re: Shouldn't FROM come before SELECT in SQL? (2011)
#106Earlier quoted context omitted.
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.
I have a very smart SQL IDE with great intellisense, but when I type "SELECT", it can't help me because it has no idea what I want. Being able to type "FROM table SELECT" would be way friendlier for humans, because IDEs could offer me immediately columns I'm very likely interested in.
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#107Earlier quoted context omitted.
In English. I think other languages would have different word orderings. German for one, and I think maybe Russian too.
The second version is how you would say it in Spanish.
"Saca las manzanas verdes del refrigerador"
Seems like the first example, vs:
"Del refrigerador, saca las manzanas verdes"
That sounds a bit stilted to me.
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#108Is the purpose of the syntax to maximize human readability or to make parsing efficient? SELECT/DELETE/INSERT etc. are commands, it makes sense to me that if I were writing a parser I would start with the imperative that will determine the rest of the path through the parser. I'm speculating, but I think I'm right, given this is late 70's/early 80's technology and resources were much less abundant.
If I remember correctly from all those Byte mags back then, having understandable queries was an important factor.
Re: Shouldn't FROM come before SELECT in SQL? (2011)
#109Earlier quoted context omitted.
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…
I believe LINQ maps to a chains of function calls on an enumerable, with the functions usually taking anonymous functions as input (and then the series of functions is rewritten into a SQL statement by EF to hit the DB). I don’t think you’d be able to support type analysis if you didn’t specify the enumerable upfront Eg Mylist.select(x => …) can determine the type of x, because it has the type of mylist —> List Selec…
select x => ... from mylist;
into mylist.SelectMany(x => ...);
is straightforward: C# compiler is not expected to be single-pass, it has an AST to operate upon. You'll have a LinqSelectNode with Projection (a lambda expression), Filter, and Source fields which you replace with a new ExtensionMethodCall{ Lhs = origNode.Source, MethodName = "SelectMany", Args = new []Node{ origNode.Projection }), easy.Re: Shouldn't FROM come before SELECT in SQL? (2011)
#110Python's syntax also rattles me for the same reason. [item for item in items if item.include] It's almost exactly like a sql statement. The order is confusing. More broadly, foreach loops are also written in the wrong order. More reasonable: foreach(items as item) In the West, we read left to right. Presenting undefined terms before defined ones burdens the mind.
Speak for yourself, I've always loved Python's list comprehension syntax, it makes ALL the sense. For ME. It's a question of ergonomics, just like my favorite chair is not going to be your favorite chair for whatever reason, programming language constructs that bug you doesn't mean it'll bug anyone else. Your foreach() example bugs the hell out of me, for example. But I'm not campaigning to have it erased from existe…
foo = 1 if true else 0
I think this was designed because most of the time foo should equal 1, and they want the code to highlight the default.
I don't like it because reading left to right it takes a few characters to realize it is a branching statement.