Earlier quoted context omitted.
It seems to be a matter of personal preference, but I've never liked single-character aliases myself, and never understood why so many seem to.
Lazy typing: t is shorter than tableWithTheDataIWantIn I prefer descriptive table and other object names, and abbreviate them in aliases within queries (though usually not to single letters).
Best practices for writing SQL queries
121–130 of 158 posts
Re: Best practices for writing SQL queries
#122This is an aside, but a colleague years back showed me his preferred method formatting SQL statements, and I've always found it to be the best in terms of readability, I just wish there was more automated tool support for this format. The idea is to line up the first value from each clause. Visually it makes it extremely easy to "chunk" the statement by clause, e.g.: SELECT a.foo, b.bar, g.zed FROM alpha a JOIN beta…
Re: Best practices for writing SQL queries
#123Earlier quoted context omitted.
My editor has tab completion, it's not like you have to type every character. I prefer it for readability but it's definitely debatable.
Readability is weird heh, I'm exactly the opposite. For me long lines become a blur, so better to have a short alias.
This is especially a problem in huge queries.
Re: Best practices for writing SQL queries
#124> Although it’s possible to join using a WHERE clause (an implicit join), prefer an explicit JOIN instead, as the ON keyword can take advantage of the database’s index. This implies that WHERE style join can't use indices. I can understand why some would prefer either syntax for readability/style reasons. But the idea that one uses indices and the other not, seems highly dubious. Looking at the postgres manual [1], t…
Re: Best practices for writing SQL queries
#125Earlier quoted context omitted.
Nearly every diff tool has -w for this, though: main main annoyance with GitHub is that I can’t enable this as the default diff mode.
Yep—`-w` is the default when I blame in my editor but ya, github is really the problem.
Re: Best practices for writing SQL queries
#126> Although it’s possible to join using a WHERE clause (an implicit join), prefer an explicit JOIN instead, as the ON keyword can take advantage of the database’s index. This implies that WHERE style join can't use indices. I can understand why some would prefer either syntax for readability/style reasons. But the idea that one uses indices and the other not, seems highly dubious. Looking at the postgres manual [1], t…
> This syntax is not as commonly used as the one above This is going to need some sources. Is it true today? And why did they put parentheses in the ON condition? Worth nothing that there were variants of the WHERE syntax to support left joins using vendor-specific operators such as A += B, A = B (+) -- those are clearly deprecated today. [1] [2] I have a really hard time finding any source on the internet that recom…
My goal was only to cast doubt on the idea that WHERE clauses in general can't use indices.
Sure, let's debate what the nicest style is. But let's not claim that our preferred style somehow makes the DB go faster (without some kind of proof).
Re: Best practices for writing SQL queries
#127Earlier quoted context omitted.
This is the style I've settled on lately, where all the major keywords are left-aligned and the clauses are consistently indented. It uses a bit more vertical space, but I find it easier to read than any other formatting style I've seen (in the wild or produced by formatters). select a.foo, b.bar, g.zed from alpha a join beta b on b.alpha_id = a.id left join gamma g on g.beta_id = b.id where a.val > 1 and b.col It's…
It's also easier if you want to comment out certain parts of your code during debugging.
Where 1 = 1
And...
And...
Which makes it easy to comment out specific filters.Re: Best practices for writing SQL queries
#128Earlier quoted context omitted.
> This syntax is not as commonly used as the one above This is going to need some sources. Is it true today? And why did they put parentheses in the ON condition? Worth nothing that there were variants of the WHERE syntax to support left joins using vendor-specific operators such as A += B, A = B (+) -- those are clearly deprecated today. [1] [2] I have a really hard time finding any source on the internet that recom…
I'm just quoting the manual here. I have no idea which style is really prevalent in the wild, now or X years ago. My goal was only to cast doubt on the idea that WHERE clauses in general can't use indices. Sure, let's debate what the nicest style is. But let's not claim that our preferred style somehow makes the DB go faster (without some kind of proof).
Re: Best practices for writing SQL queries
#129"a = 'foo'" is exactly the same performance as "a like 'foo'" and very close to the performance as "a like 'foo%'" and is fully indexed. When you put a wildcard in the front, the entire index is avoided, so you gotta switch to full text search.
Re: Best practices for writing SQL queries
#130This is an aside, but a colleague years back showed me his preferred method formatting SQL statements, and I've always found it to be the best in terms of readability, I just wish there was more automated tool support for this format. The idea is to line up the first value from each clause. Visually it makes it extremely easy to "chunk" the statement by clause, e.g.: SELECT a.foo, b.bar, g.zed FROM alpha a JOIN beta…