SQL Best Practises
Here are my high best practises for writing SQL queries. These are to make the code more readable and transferable.
- always make all SQL key words uppercase
- use inline comments
- use tabs instead of spaces
- indent all subqueries
- use AND 101
- very important for readability and debugging
- split column names to new lines
- never use select *, instead select the columns you want and give each column name on a new line with the comma before. This makes debugging and query modification much easier.
- add empty lines for complex queries
- this is to allow different parts of the query to have their own space.
- do not shorten table name aliases
- an alias given to a table name should be the table name itself. There is no need to shorten in. The reason for this is that table name aliases choices are very subjective and are not at all consistent. An alias name in one context may not have the same name as an alias name in another context and will lead to confusion eventually.