Re:Literate SQL
by joshka on Saturday December 19, 2015 @04:05PM
(#10764837)
A somewhat related tip that works in SqlServer (not sure about other dialects) is to put the column alias first rather than at the end of a calculated column. E.g. rather than:
Use instead:
The advantage here is that the names also line up neatly and the shape (column names etc.) of the result is obvious.
[ Parent
| on HN ]
Re:Literate SQL
by rupert_murdaaa on Saturday December 19, 2015 @05:17PM
(#10765110)
I'm so used to using and reading the "AS" syntax for aliasing that if I came across this it'd throw me off.
It seems to me that sticking everything in CTEs would be equally as jarring. Writing SQL or reading and understanding someone else's SQL is almost always a non-linear process, not least of all because the processing order by the database engine isn't top-to-bottom.
It took a minute for me to accept it as well, but once I did the queries just look so much better.
Is that standard SQL?
Likely not. My brief digging into trying to find a definitive document on what is 'standard SQL' led me down a rabbit hole and I gave up on working out whether it was or wasn't. The likelihood of having to change to a different provider on any project that has settled on SqlServer in my experience is fairly close to 0. YMMV