If yoda you were, SQL you would invent
SQL is such a bizarre language. It is all backwards. Consider:
SELECT item_id, COUNT(*) AS qty, SUM(qty * price) AS total
FROM line_item
WHERE invoice_id = 18743
GROUP BY item_id
HAVING SUM(discount) > 0
ORDER BY item_id;
What is the logical flow of this statement? If SQL were written in logical-flow order, it might look something like this:
FROM line_item
WHERE invoice_id = 18743
RETRIEVE item_id, qty, price, discount
GROUP BY item_id
COMPUTE item_id, COUNT(*) AS qty, SUM(qty * price) AS total, SUM(discount)
HAVING SUM(discount) > 0
PROJECT item_id, qty, total
ORDER BY item_id;
SQL can get really hard to read and understand, really fast. Faster than any other language I can think of, and I’ve been programming in SQL for years. It’s such a weird language. It’s almost declarative, but it’s not. It feels a little pseudo-functional, like LISP, but it’s not. Even XSLT makes more sense than SQL!
How many fewer bugs would SQL programmers write if they didn’t have to do a mental translation between the Yoda-speak that is SQL and the logical operations the RDBMS executes? I’m not saying they should know the actual implementation, I’m just saying it’d be great if we didn’t obfuscate things more than needed.
But George Lucas wouldn’t like that, and I guess he was on the SQL committee.

Comments, you have? MMmmmmmmmrmrrrrr! Leave them below, you will!



When years of experience you acquire, natural this pattern becomes.
Tyler
1 Feb 13 at 11:46 am
The SELECT statement never bothered me too much, but I have always wanted to rewrite UPDATE to accept:
UPDATE t
WHERE c = 1
SET c = 2;
Forcing people to specify the WHERE clause before the SET clause would have probably saved the world billions in $ / hours / etc… just be getting rid of accidental updates without where clauses.
Robert Treat
1 Feb 13 at 11:46 am
Funny!
One of the things I’ve liked about Microsoft’s Linq is that while it looks a bit like SQL to increase familiarity, they did alter the ordering of statements very much in the way you’re describing. As someone who was much more familiar with SQL, this looked weird at first, but it started to make more sense over time.
Ashish Tonse
1 Feb 13 at 11:57 am
Amen Robert. Excellent idea.
Xaprb
1 Feb 13 at 12:10 pm
Tyler, 800 years of experience you have?
Xaprb
1 Feb 13 at 12:11 pm
May be it is a matter of opinion but the way I looked at it is:
What I want,
From where I want,
How I want,
how I want it to be presented
so something like,
sed -ne ’1,10p’ file | grep pattern | sort
So, this fits a data flow paradigm, but again for complex large queries it can get really hard to mentally parse I guess. :) (But that is there in latter representation as well)
Raghavendra Prabhu
3 Feb 13 at 5:35 pm
Hi Baron,
Good point, you make.
Unrelated to SQL, tell me which software or website you used to create the image, could you ?
Romain
4 Feb 13 at 11:54 am
Mmmmmrrrrm! memegenerator.net, it was.
Xaprb
4 Feb 13 at 12:41 pm
Thanks :)
Romain
4 Feb 13 at 1:09 pm