SQL Comments Guide: Best Practices & Usage
In SQL, comments can be used to provide explanations or notes for the code. They can briefly describe the purpose, function, or other relevant information of the SQL query, making it easier for other developers to understand the code.
There are two types of comments in SQL: single-line comments and multi-line comments.
- Single-line comment: Use double dashes (–) to indicate a comment, the content following will be treated as a comment. For example:
SELECT * FROM customers -- This is a comment
WHERE city = 'New York';
- At the beginning, asterisk and slash (/)
/* This is a multi-line
comment that spans
across multiple lines */
SELECT * FROM customers
WHERE city = 'New York';
Adding proper comments can enhance the readability and maintainability of code, it is recommended to include comments appropriately when writing SQL code for others to understand.