SQL Comments
A comment is a programmer-readable explanation or annotation used in the SQL statements that do not affect their execution. It is used to make SQL statements easier to understand for humans. We can include the comment in between any keywords, parameters, or punctuation marks in a statement. During the parsing of SQL code, the SQL Server engine usually ignores them. It matters a lot in SQL queries like programming languages.
Some examples of using comments are:
– It can be used to label a section of code to make it easier to understand for others.
– It also allows us to modify things later when we remove a column shown in the output of a SELECT statement but still keep the name in the query.
– To check data and performance, disable lines of a WHERE clause.
SQL Server provides three types of comments, which are given below:
1. single-line Line Comment
2. Multiline Comment
Single-Line Comments
Comments are mostly used to document the code and provide descriptions of what it does. Let us describe each type of comment in detail with examples.
A comment that starts and ends in a single line is known as a single-line comment. We can add comments with the same SQL statement, nested at the end of the SQL statement or on a separate line. The SQL Server engine does not evaluate the comments. Single line comments are represented by the double dash (- -) symbol.
We can use the single-line comment as below:
— the text of comment written here
Example:
The following statement is executed correctly and displayed the employee details:
— This command displays employee information
SELECT * FROM Employee;
Executing the statement will give the below output without parsing comment:
Course Video
NOTE: Practice below practice questions on MSSQL SERVER, it will not execute on site editor.
1. Write a single line comment to describe the purpose of the following SQL query:
2. Write a multi-line comment to describe the purpose of the following SQL query:
3. In below query Comment FirstName column in the query :
SQL comments are lines or blocks of text within SQL code that are ignored by the database engine. They are used to explain code or leave notes for developers.
SQL supports two types of comments:
Single-line comments: Start with —
Example:
sql
Copy code
SELECT * FROM Employees; — Fetches all employee data
Multi-line comments: Enclosed between /* and */
Example:
sql
Copy code
/*
This query fetches all employees
who joined after 2022
*/
SELECT * FROM Employees WHERE join_date > ‘2022-01-01’;
Keep comments concise and relevant.
Use comments to explain complex logic.
Avoid excessive commenting; focus on clarity.
Use consistent formatting across the team.
Comments provide clarity on the purpose of queries, making it easier for team members to understand and maintain the codebase. Example:
sql
Copy code
— Calculates total sales for the current month
SELECT SUM(amount) FROM Sales WHERE MONTH(sale_date) = MONTH(GETDATE());
Yes, comments can be added to stored procedures to explain their functionality or specific sections. Example:
sql
Copy code
CREATE PROCEDURE GetActiveEmployees
AS
BEGIN
/* Fetch employees with active status */
SELECT * FROM Employees WHERE status = ‘Active’;
END;
Including outdated or incorrect comments.
Over-commenting simple queries.
Using comments to explain poor code instead of improving the code.
Yes, explore our SQL Comments Tutorials Online for Beginners for tips and examples.
No, comments are ignored by the database engine and have no impact on query performance.
Yes, you can comment out parts of a query to isolate issues during debugging. Example:
sql
Copy code
— SELECT * FROM Employees;
SELECT name, department FROM Employees;
Comments improve code readability, help with debugging, and ensure better collaboration among developers, especially in large projects.