Here are some SQL programming guidelines and best practices we collected, keeping quality, performance and maintainability in mind. This list is not complete at this moment, and will be constantly updated.
- Do not use SELECT * in your queries.
- Always use table aliases when your SQL statement involves more than one source.
- Use the more readable ANSI-Standard Join clauses instead of the old style joins.
- Do not use column numbers in the ORDER BY clause.
- Always use a column list in your INSERT statements.
- Don’t ever use double quotes in your T-SQL code.
- Do not prefix your stored procedure names with “sp_”.
- Always use a SQL formatter to format your sql like Instant SQL Formatter(Free and Online)
Do not use SELECT * in your queries, write out the full syntax.Always write the required column names after the SELECT statement, like:
SELECT CustomerID, CustomerFirstName, City from Emp;This technique results in reduced disk I/O and better performance.
Always use table aliases when your SQL statement involves more than one sourceIf more than one table is involved in a from clause, each column name must be qualified using either the complete table name or an alias. The alias is preferred. It is more human readable to use aliases instead of writing columns with no table information.
Use the more readable ANSI-Standard Join clauses instead of the old style joinsWith ANSI joins, the WHERE clause is used only for filtering data. Where as with older style joins, the WHERE clause handles both the join condition and filtering data. Furthermore ANSI join syntax supports the full outer join. The first of the following two queries shows the old style join, while the second one shows the new ANSI join syntax:
FROM TITLES t,
WHERE a.Au_id = ta.Au_id
AND ta.Title_id = t.Title_id
AND t.Title LIKE ‘%Computer%’
FROM AUTHORS a
INNER JOIN TITLEAUTHOR ta
ON a.Au_id = ta.Au_id
INNER JOIN TITLES t
ON ta.Title_id = t.Title_id
WHERE t.Title LIKE ‘%Computer%’
Do not use column numbers in the ORDER BY clauseAlways use column names in an order by clause. Avoid positional references. Consider the following example in which the second query is more readable than the first one:
ORDER BY 2
SELECT OrderID, OrderDate
ORDER BY OrderDate
Always use a column list in your INSERT statementsAlways specify the target columns when executing an insert command. This helps in avoiding problems when the table structure changes (like adding or dropping a column). Consider the following table:
Countryid INT PRIMARY KEY,
VALUES (1, ‘Ireland’)
ADD EuroSupport bit
VALUES (1, ‘England’)
Don't ever use double quotes in your T-SQL codeUse single quotes for string constants. If it's necessary to qualify an object name, use (non-ANSI SQL standard) brackets around the name, like table name: ORDER DETAILS in this SQL.
FROM [northwind].[dbo].[ORDER DETAILS] AS od
Do not prefix your stored procedure names with “sp_”The prefix sp_ is reserved for system stored procedure that ship with SQL Server. Whenever SQL Server encounters a procedure name starting with sp_, it first tries to locate the procedure in the master database, then it looks for any qualifiers (database, owner) provided, then it tries dbo as the owner. So you can really save time in locating the stored procedure by avoiding the “sp_” prefix.
Always use a SQL formatter to format your sql like Instant SQL Formatter(Free and Online)
The formatting of SQL code may not seem that important,but consistent formatting makes it easier for others to scan and understand your code.
SQL statements have a structure, and having that structure be visually evident makes it much easier to locate and verify various parts of the statements.
Uniform formatting also makes it much easier to add sections to and remove them from complex T-SQL statements for debugging purposes.
Instant SQL Formatter is a free online SQL tidy tool that makes your SQL script readable instantly.