When it comes to working with SQL databases, commenting can be an essential tool for developers and database administrators alike. Comments in SQL allow you to add notes to your code, explain sections of your queries, and prevent certain lines of code from executing. By adding comments to your SQL code, you can make it more readable, maintainable, and easier to understand for yourself and others.
SQL comments come in two forms: single-line comments and multi-line comments (also known as block comments). Single-line comments begin with two dashes (–), while multi-line comments are enclosed in forward slash and asterisk (/* and */). Both types of comments can be used to document your SQL code, but multi-line comments are generally better suited for longer explanations or comments that span multiple lines. Additionally, some SQL dialects, such as MySQL, also allow the use of the hash symbol (#) for single-line comments.
What is SQL Comment?
SQL Comment is a feature that allows developers to add notes and explanations to their SQL code without affecting the execution of the code. Comments can be used to describe the purpose of the code, explain complex queries, or simply add reminders to the developer.
SQL Comments Syntax
In SQL, comments are created by starting the comment with two dashes (–) for single-line comments, or by enclosing the comment between /* and */ for multi-line comments. Here’s an example of a single-line comment:
SELECT * FROM customers -- This is a comment
And here’s an example of a multi-line comment:
/*
This is a multi-line comment
It can span multiple lines
*/
Types of SQL Comments
There are two types of SQL comments: single-line comments and multi-line comments.
Single-line Comments
Single-line comments are used to add comments to a single line of code. They start with two dashes (–) and continue until the end of the line. Here’s an example:
SELECT * FROM customers -- This is a comment
Single-line comments are useful for adding short comments to a line of code.
Multi-line Comments
Multi-line comments are used to add comments that span multiple lines of code. They start with /* and end with */. Here’s an example:
/*
This is a multi-line comment
It can span multiple lines
*/
Multi-line comments are useful for adding longer comments that explain complex code.
In conclusion, SQL comments are a valuable feature of SQL that allows developers to add notes and explanations to their code without affecting the execution of the code. Single-line and multi-line comments are the two types of comments that can be used in SQL, and they can be used to add short or long comments to the code.
Why Use SQL Comments?
SQL comments are essential for any developer who wants to write clean, readable, and maintainable code. Here are some reasons why developers should use SQL comments:
1. Explain Code Sections
Comments are used to explain sections of SQL statements. They help developers understand what a particular section of code does. For example, a comment can be added to explain a complex SELECT statement. This makes it easier for other developers to read and understand the code.
2. Prevent Execution of Code
Comments can also be used to prevent the execution of SQL statements. For example, if a developer wants to test a new SQL statement without deleting the existing code, they can comment out the old code. This ensures that the old code is not executed when the SQL statement is run.
3. Improve Code Readability
SQL comments improve code readability. A well-commented code is easier to read and understand. This is especially useful when working on large projects with multiple developers. Comments also help developers remember why they wrote a particular piece of code.
4. Document Code Changes
Comments can be used to document code changes. When a developer makes changes to an existing SQL statement, they can add a comment to explain what they changed and why. This helps other developers understand the changes and why they were made.
5. Best Practice
Using comments in SQL is considered a best practice. It is a way to ensure that the code is readable, maintainable, and understandable. SQL comments are also useful when debugging code. Developers can use comments to isolate a particular section of code and test it separately.
In conclusion, SQL comments are an essential tool for any developer who wants to write clean, readable, and maintainable code. They help explain code sections, prevent execution of code, improve code readability, document code changes, and are considered a best practice in SQL development.

How to Comment in SQL
When working with SQL, it’s important to add comments to your code to make it more understandable and maintainable. Comments in SQL are used to explain the purpose of the code, add notes, and provide context to other developers who may read the code. In this section, we’ll cover the different ways to add comments in SQL.
Single-Line Comments
Single-line comments in SQL are used to comment out a single line of code. To add a single-line comment, use two dashes “–” followed by the comment text. Anything after the two dashes will be ignored by the SQL engine. Here’s an example:
SELECT * FROM Customers -- This is a comment
In the above example, the SQL engine will only execute the SELECT statement and ignore the comment text after the two dashes.
Multi-Line Comments
Multi-line comments in SQL are used to comment out multiple lines of code. To add a multi-line comment, use “/” to start the comment and “/” to end the comment. Anything between the “/” and “/” will be ignored by the SQL engine. Here’s an example:
/*
This is a multi-line comment
that spans across multiple lines
*/
SELECT * FROM Customers
In the above example, the SQL engine will ignore the entire multi-line comment and only execute the SELECT statement.
Uncommenting SQL Code
To uncomment SQL code, simply remove the comment syntax from the code. For single-line comments, delete the two dashes “–” and the comment text. For multi-line comments, delete the “/” and “/” and the comment text in between.
It’s important to note that nested comments are not supported in SQL. If you try to nest comments, you’ll get a syntax error.
In summary, comments in SQL are a powerful tool for making your code more understandable and maintainable. By using single-line and multi-line comments, you can add context and notes to your code. When commenting out code, use the appropriate syntax for single-line and multi-line comments. And remember, nested comments are not supported in SQL.
Best Practices for SQL Comments
When writing SQL code, it is important to include comments to improve readability and maintainability. Here are some best practices to follow when adding comments to your SQL code:
Use descriptive comments
Comments should provide context and explain the purpose of the code. Use descriptive comments that explain the logic behind the code. This will help other developers understand the code and make it easier to maintain in the future. For example, instead of using a comment like -- get data
, use -- retrieve all student email addresses from the database
.
Use consistent formatting
Consistent formatting makes code easier to read and understand. Use the same formatting for all comments in your code. For example, if you use block comments (/* */
) for one comment, use them for all comments in your code.
Avoid unnecessary comments
While comments are helpful, too many comments can make code difficult to read. Avoid commenting on obvious or self-explanatory code. Only add comments when they add value to the code.
Use single-line comments for short comments
Single-line comments (--
) are useful for short comments that fit on one line. Use single-line comments to explain specific lines of code or to add context to a query. For example, -- join student and email tables on student ID
or -- filter results to only include students who were last updated within the last month
.
Use block comments for longer comments
Block comments (/* */
) are useful for longer comments that span multiple lines. Use block comments to explain the purpose of a stored procedure or function, or to provide an overview of a complex query. For example:
/*
This stored procedure retrieves all student email addresses from the database
and returns them as a comma-separated list.
*/
By following these best practices, you can make your SQL code easier to read and understand, which will save time and effort in the long run.
Cristian G. Guasch
Hey! I'm Cristian Gonzalez, I created SQL Easy while I was working at StubHub (an eBay company) to help me and my workmates learn SQL easily and fast.
Related articles
- Why Use WHERE 1=1 in SQL Queries? Exploring Its Impact on Database Efficiency
- How to Create a Table in SQL? Your Step-by-Step Guide for Beginners
- How to Use GROUP BY in SQL? Master the Art of Query Optimization
- How to Use UPDATE in SQL: A Comprehensive Guide for Beginners
- How to Use Select in SQL: A Beginner’s Guide to Database Queries
- How to Use Select Distinct in SQL: A Simple Guide for Efficient Database Queries
- How to Use Union in SQL: A Simple Guide for Efficient Database Management
- How to Use Self Join in SQL: A Comprehensive Guide for Beginners
- How to Use Full Join in SQL: A Comprehensive Guide for Beginners
- How to Use Right Join in SQL: A Comprehensive Guide for Database Enthusiasts
- How to Use Left Join in SQL: A Guide for Database Query Optimization
- How to Use INNER JOIN in SQL: A Simple Guide for Efficient Database Queries
- How to Use Joins in SQL: A Comprehensive Guide for Database Enthusiasts
- How to Use Null Values in SQL? A Comprehensive Guide for Beginners
- How to Use INSERT INTO in SQL: A Comprehensive Guide for Beginners
- How to Add Ranking Positions of Rows in SQL with RANK(): A Simple Guide
- How to Divide one Column by Another in SQL – Quick Tricks for PostgreSQL and SQLite
- How to Run SQL Script: A Comprehensive Guide
- How to Use SQL in Python: A Comprehensive Guide
- How to Count in SQL: A Quick Guide to Mastering Queries
- How to Drop a Column in SQL: Practical Guide for Database Optimization
- How to Backup SQL Database: A Comprehensive Guide
- How to Compare Dates in SQL: A Quick and Efficient Guide
- How to View a Table in SQL: Essential Steps for Database Inspections
- How to Create Index in SQL: A Concise Guide for Database Optimization
- How to Sort in SQL: Mastering ORDER BY for Efficient Queries
- How to Improve SQL Query Performance: Expert Tips and Techniques
- How to Update Multiple Columns in SQL: Efficient Techniques and Tips
- How to Rename a Table in SQL: Quick and Easy Steps
- How to Count Rows in SQL: A Simple and Efficient Guide
- How to Count Distinct Values in SQL: A Comprehensive Guide
- How to Use CASE in SQL: Practical Tips and Examples
- How to Prevent SQL Injection Attacks: Essential Tips and Best Practices
- How to Use SQL in Excel: Unleashing Data Analysis Capabilities
- How to Join 3 Tables in SQL: Simplified Techniques for Efficient Queries
- How to Pivot in SQL: Mastering Data Transformation Techniques
- How to Create a Temp Table in SQL: A Quick Guide
- How to Insert Date in SQL: Essential Tips for Database Management
- How to Rename a Column in SQL: Step-by-Step Guide
- How to Run a SQL Query: Your Ultimate Guide
- How to Delete a Row in SQL: Easy Steps for Quick Results
- How to Join Multiple Tables in SQL: A Beginner’s Guide
- Optimizing SQL Queries: A Comprehensive Guide
- How to Join Two Tables in SQL: A Step-by-Step Guide
- What is SQL and How to Use It?
- How to Remove Duplicates in SQL: A Step-by-Step Guide
- Adding a Column in SQL: A Quick and Easy Guide
- How to Find Duplicates in SQL: A Step-by-Step Guide