How to Comment in SQL ?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
By Srikanth Varma
Free
star5
Enrolled: 1000
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
Srikanth Varma
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

It might be challenging to determine who updated a particular query and why because databases typically contain numerous lines of SQL statements, and different developers frequently alter the various queries that are contained in them. Here comes the SQL comments. SQL comments allow the developer to read and understand the logic behind the queries.

As you can add comments in languages like C++, Python, and other programming languages, SQL also allows you to write comments in SQL. For example, you can add a comment before a specific query that explains the logic and insights of the query and why it is written in a particular place. SQL comments can appear on a single line or multiple lines based on the description or the length of a comment. Let's deep dive into how you can write comments in SQL.

What are SQL Comments?

As mentioned above, SQL comments help you or any developer on the team to understand and maintain the SQL queries. Similar to how comments function in other programming languages, SQL does not execute these comments, hence the queries are unaffected.

There are three ways in which you can add the SQL comments :

Single Line Comment

The SQL allows you to make single-line comments by using the two dashes (--). The SQL server ignores the text that appears after the two dashes on a single line.

The query shown below illustrates how to create comments in SQL on a single line.

Multi-Line Comment

The single-line comments work only if the comment is of a single line. If you want to add a large comment that spans across multiple lines then you have to put a double dash on every single line. But there is another way and more efficient way to add multi-line comments in SQL.

You can add the multi-line comments in the SQL that starts with the /* and ends with the */. The content or the description written inside these is ignored by the SQL server. The below-given query explains how you can add multi-line comments in SQL.

Inline Comment

The third type of comment is Inline comments and which is an extension of the multi-line comment. Inline comments in SQL can be added between the SQL statements and are enclosed within /* and */. The below-given query explains how you can add inline comments in SQL.

SQL Comment Indicators

SQL comment indicators include the double hyphen ( -- ), braces ( { } ), and C-style comment delimiters i.e (/_ ... _/).

You can add comment indicators using shortcuts available in the SQL server.

You can turn existing queries or some existing text into comments. First, select the text in the SQL comments, and then press Ctrl + forward slash ( / ) to add double hyphen comment indicators.

To remove the comment indicators, first, select the SQL statements and then press Ctrl + forward slash ( / ). Remember that if any text is not selected, the comment indicator is added to the beginning of the current line.

Comments within SQL Statements

A SQL statement can contain multiple comments of both types as mentioned above. Let's see an example below that contains many comments in a single SQL query.

This is how you can use comments within the SQL statements.

Comments on Schema Objects

You can also add comments on schema objects like table, view, materialized view, columns, etc.

To add a comment to a table, column, or any view, you can use the COMMENT statement. And to drop the comments from the database, set it to the empty strings i.e. ''. Now, let's take a look at an example in which you can add a comment on the emp_id column of the Sales table.

The above SQL query is used to add comments in SQL on schema objects. Now, let's take a look at how you can remove the same comment from the database.

The above query denotes that setting the comment to '' removes the comment from the selected schema object.

Conclusion

  • As you can add comments in languages like C++, Python, and other programming languages, SQL also allows you to write comments in SQL.
  • You can add the single-line comments in the SQL server using the two dashes( -- ).
  • Similar to how comments function in other programming languages, SQL does not execute these comments, therefore the queries are unaffected.
  • You can add the multi-line comments in the SQL server that starts with the /* and ends with the */.
  • You can turn existing queries or some existing text into comments using Ctrl + forward slash ( / ) to add double hyphen comment indicators.

Learn More