r/SQL • • 19d ago

Discussion Why do we keep writing sql backwards?

It is a common consensus that sql is kind of written backwards than one would expect, instead of first defining the place you want to get somehting from and then what it is you want, like literally any other language out there, you first select the content and then define where you want it to be taken from

This leads to increadibly akward syntax when writing something more complex than "select x from y where z" and is literally opposite to the actual process of the machine.

This is all because some dumbass, excuse my language, in the 70's decided it should follow english grammar for some reason, and in doing so he made the most clunky writing system ever.

Now this at the time may not have been that bad, but from a few searches and posts here i saw that most people agree that this is a clunky, akward and unpleasant way of typing. So, why not switch it around? Why not change the standard to something more usable?

This is kind of a rant because i have been learning sql for collage and the syntax has been INCREADIBLY frustrating

0 Upvotes

30 comments sorted by

View all comments

Show parent comment

-3

u/MrDeadMeme 19d ago

I dont know, to me it is backwards. At the end of the day though, in order to write good code you kind need to be able to think like the computer, so you can visualize the efficiency of your commands to it better(whether that is time complexity, unnecessery repeats of code etc) and having to think like a computer and then switch back to write like a human is 10 times harder than putting that thought down, exactly as is.

3

u/big_swede 19d ago

But do you have a real grasp of how the DBMS takes the query, breaks it down and creates an execution plan and actually fetches the data? 

I didn't think that was part of the curriculum of a college course.

I have had some positions where I worked very closely with DBA's and accomplished DB devs and even they said that they didn't know the inner workings of the DBMS.

1

u/GTS_84 19d ago

Except you aren't thinking like a computer, ever, with SQL. It is making a lot of decisions about how to run the code most efficiently, you don't even get a say in how it runs.

I mean, yeah, it can be clunky, especially when you get beyond the basics. The order might make sense in english when it's a simple "select columns from table where" type statement, but once you start throwing in aggregates, and CTE's, and who knows what the fuck else it really stops making sense from a language perspective. But don't kid yourself into thinking that changing the order would actually help you "think like a computer"

There are absolutely languages where you need to think like a computer, SQL ain't one of them, and you don't need to think like a computer to write good code in SQL.

-1

u/MrDeadMeme 19d ago

I mean, changing the order to match the way the code executes rather than english grammar i think is the most straightforward way to make it write like the computer "thinks" i dont get how it wouldnt help

2

u/GTS_84 19d ago

Two things.

First, there isn't a set order. What you think of as the execution order is more like the most common order for a lot of basic statements. The optimizer will determine the best order and depending on the specifics of the statement will determine the best order. So for oracle for example, where tables are stored in a heap and indexes are in a separate B Tree index, whether or not your "WHERE" clause filters on an indexed column or not can change the execution order. And that's just one example. Now, to be clear, you are probably not expected to understand this at this juncture, but going back to my earlier comment about SQL being a declarative language and not an imperative language, there is a lot being determined under the hood that you have no say over. You aren't even wrong about the current order being kind of fucked, but your solution isn't any better, especially because it would probably suck to read. Which gets to my next point.

Second, you are talking about writing SQL a lot, and how writing SQL could be easier, and I would respectfully suggest you stop thinking about it in that way. It's far more important that SQL be easy to read rather than easy to write. Even if you land on the same conclusions, coming at it from the perspective of "What will be easiest for everyone else to read" instead of "What will be easiest for me to write" will lead you down a more fruitful path. You will write a statement once, it may be read many times over.