News for hackers. Stuff that matters.
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:

  SELECT
    Foo,
    Sum(Baz) AS Bar
  FROM Quux
Use instead:

  SELECT
    Foo,
    Bar = Sum(Baz)
  FROM Quux
The advantage here is that the names also line up neatly and the shape (column names etc.) of the result is obvious.
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.

Re:Literate SQL by joshka on Monday December 21, 2015 @02:40PM (#10772955)
It took a minute for me to accept it as well, but once I did the queries just look so much better.
Re:Literate SQL by chris_wot on Saturday December 19, 2015 @05:05PM (#10765079)
Is that standard SQL?
Re:Literate SQL by joshka on Monday December 21, 2015 @02:39PM (#10772940)
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