TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How I format SQL code

60 pointsby szaboatover 5 years ago

13 comments

lancefisherover 5 years ago
I appreciate write-ups like this, but I really disagree with what seems to be the majority that SQL keywords should be uppercase. It’s one of the last uppercase holdovers from the old days. HTML used to be uppercase as well. Lowercase is objectively more readable, easier to type, and editors colorize keywords so they stand out. Uppercase is really not necessary in the 2020s. Check out Matt Mazur’s styleguide (linked in the post) for an alternative that endorses lowercase. He also has a contrasting style on where Boolean operators should go. <a href="https:&#x2F;&#x2F;github.com&#x2F;mattm&#x2F;sql-style-guide&#x2F;blob&#x2F;master&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mattm&#x2F;sql-style-guide&#x2F;blob&#x2F;master&#x2F;README....</a>
评论 #22192681 未加载
评论 #22197833 未加载
评论 #22192503 未加载
评论 #22193216 未加载
评论 #22197209 未加载
jolmgover 5 years ago
&gt; God is merciful because AND_ is 4 characters, a good tab width, so WHERE conditions are to be lined up like (same for JOIN conditions)<p><pre><code> WHERE country = &#x27;UAE&#x27; AND day &gt;= DATE(&#x27;2019-07-01&#x27;) AND DAY_OF_WEEK(day) != 5 AND scheduled_accuracy_meters &lt;= 10*1000 </code></pre> It looks better when you use a tab-width of 2:<p><pre><code> WHERE country = &#x27;UAE&#x27; AND day &gt;= DATE(&#x27;2019-07-01&#x27;) AND DAY_OF_WEEK(day) != 5 AND scheduled_accuracy_meters &lt;= 10*1000</code></pre>
评论 #22194640 未加载
dchessover 5 years ago
I don&#x27;t see the benefit of putting table names on a different line than the keyword.<p>How is this:<p><pre><code> FROM tablename t INNER JOIN other_table ot ON t.id = ot.id </code></pre> More readable than:<p><pre><code> FROM tablename t INNER JOIN other_table ot ON t.id = ot.id </code></pre> I agree with a lot of these recommendations, but this one irks me. Also I&#x27;d love if someone could create a nice code-formatter for SQL like Python&#x27;s Black.
评论 #22196993 未加载
monkeycantypeover 5 years ago
I also use his multi line format for boolean logic:<p><pre><code> select &#x27;biscuit&#x27; where ( ( @alpha &lt; pow( sin( radians( @scheduled_lat - @actual_lat ) &#x2F; 2 ) , 2 ) ) and @alpha &gt; 0 )</code></pre>
flatfilefanover 5 years ago
Great style guide in my opinion. It is actually rather helpful to have those SQLs formatted neatly. As an analyst you have to write quite a few of them. So copy pasting and reusing is most helpful and boosts productivity. To make sure that you don’t make errors a clean layout for eyeballing is necessary. The same for bug fixing, should you have one planted still.
truculentover 5 years ago
If it doesn’t come with an auto formatter it doesn’t matter. Making developers manually style their code is barbarism
whynotmaybeover 5 years ago
Personaly, I put the comma before the column name :<p><pre><code> SELECT col1 ,col2 ,col3 </code></pre> It&#x27;s easier for me to add a column or move it like this. Otherwise I have to search the comma when my query has only one column and I add one or when I add a column at the end
评论 #22194924 未加载
merusameover 5 years ago
I struggle to find a beautifier doing something similar to this with indentation. I use quite a bit of plpgsql which makes it even more challenging. I have tried a few found in the www however none of them cut it. Any recommendations?
arh68over 5 years ago
This is my favorite guide yet!<p>My syntax, like others, is a little different (lowercase, 2 spaces, commas-first, bracket quotes, ons right under joins w&#x2F; joined table on LHS, left joins left-aligned): (this query isn&#x27;t supposed to make sense)<p><pre><code> select u.id [user] , u.email [email] , o.name [office] , sum(t.id) [# things] from main_tblusers_db u inner join tbloffices_db o on o.id = u.office_id inner join things_tbl t on t.user_id = u.id left join example e on e.user_id = u.id where u.deleted is null and ( u.active is not null or u.special = 1 ) group by u.id -- the 1, 2 syntax is new to me! , u.email , o.name</code></pre>
评论 #22197853 未加载
评论 #22197055 未加载
leblancfgover 5 years ago
The rest of this man&#x27;s blog is also worth a visit. Great work, Marton!<p>P.S. Can I suggest you put your name somewhere in your header?<p>P.P.S. I see you, too, use &#x27;self&#x27; when taking notes. Would you also be a Pythonista? :)
wodenokotoover 5 years ago
Can anyone explain the logic &#x2F; benefit of the group by recommendation?
评论 #22193184 未加载
ninjuover 5 years ago
It&#x27;s a case of yet another standard (<a href="https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;</a>)<p>The author recommends using upper-case for all keywords while Matt Mazur&#x27;s SQL style guide, that is linked at the bottom of the article, recommends using lowercase for keywords :-)
vladsanchezover 5 years ago
I&#x27;ve done it that way for the last 20 years, but I&#x27;ve never blogged&#x2F;wrote about it. That&#x27;s the difference.
评论 #22194427 未加载