What is the accepted practice for indenting SQL statements? For example, consider the following SQL statement:
SELECT column1, column2 FROM table1 WHERE column3 IN ( SELECT TOP(1) column4 FROM table2 INNER JOIN table3 ON table2.column1 = table3.column1 )
How should this be indented? Many thanks.
Indenting makes SQL easier to follow, as it makes it visually structured. It's recommended not to indent the first line in a multiple line statement, so it would be clear where the statement starts. Make sure that the SQL left margin is indented per the section nesting.
If the name is the same as an SQL keyword, enclose the name within quotation marks. The name of an object in a database for a table or a column should be unique and not too long. Avoid special characters in the name like $, &, * , etc. (use only letters, numbers, and underscores).
Whitespace is optional in pretty much any language where it is not absolutely necessary to preserve boundaries between keywords and/or identifiers. You could write code in C# that looked similar to your SQL, and as long as the compiler can still parse the identifiers and keywords, it doesn't care.
SELECT column1 , column2 FROM table1 WHERE column3 IN ( SELECT TOP(1) column4 FROM table2 INNER JOIN table3 ON table2.column1 = table3.column1 )
I like to have all "," in front, this way I never search them when an error at line X from the SQL editor.
SELECT sdcolumn123 , dscolumn234 , sdcolumn343 , ffcolumn434 , sdcolumn543 , bvcolumn645 vccolumn754 , cccolumn834 , vvcolumn954 , cvcolumn104 FROM table1 WHERE column3 IN ( ... ) SELECT sdcolumn123, dscolumn234, asdcolumn345, dscolumn456, ascolumn554, gfcolumn645 sdcolumn754, fdcolumn845, sdcolumn954, fdcolumn1054 FROM table1 WHERE column3 IN ( ... )
I found easier and more quick at the first example. Hope this example show you more my point of view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With