Some SQL coding Recommendations
When writing SQL code, we would like all our queries to execute without issues. That is always the goal, but what if you write a single line of a query? Do you think it is a good thing maybe because it will execute? No, we should make it readable. In this article, I am highlighting some SQL coding recommendations and samples:
- Capitalize all keywords. These include SELECT, FROM, WHERE, ORDER BY, etc.
- Lower all other codes that are not SQL Keywords. These include column names, variables used in where clauses, etc.
- Make use of comments only for code that is difficult to understand. One thing to remember is, explain why's and not what your code is doing.
- Always update comments when code is updated. I will not update this comment because someone else added it? Mmmmmhhh, no no no... Avoid that. Always makes sure when you make a change in your SQL statement and it was commented on, make sure it is always relevant to your current changes.
- Start Each clause in a new line. A single line of a query will look bad even when keywords are capitalized and other codes are lowercase. It is good to separate clauses when writing SQL statements.
- Break long clauses into multiple lines. What do you mean? Did you not say, Start each clause in a new line? Yes, I did. But, let's take the example of having multiple conditions in a WHERE clause? In such instance, for more readable code, it is good to do a multi line breaking.
- Indent continued lines. Last but not least, always for a continuous line such as those resulting from breaking long clauses, make sure they are intended.
Thanks for reading this and I hope you took something home. Happy coding in SQL programming.
Software Engineer at Eversend
4 年Great insight