Home »
SQL
SQL Comments
Comments in SQL: In this tutorial, we are going to learn about the comments in SQL (Structured Query Language), types of the comments in SQL.
Submitted by Abhishek Goel, on March 20, 2020
A comment in any text which is ignored by the compiler and is not executed. It is used to convey some information to the user and used for documentation only, to describe the command's purpose in an application. Comments can be placed anywhere in the statement between any keywords, parameters or punctuation mark in a statement.
Types of SQL Comments
- Line Comments
- Block Comments
1) Line Comments
- Line comments beginning with two dashes (--):
Line or Single-Line comments start with two dashes (--) and end with carriage returns or line break. Any text between two dashes (--) and end of the line is considered as a comment. These comments can be placed anywhere in the code and everything before dashes (--) will execute like normal MySQL statements and the text after two dashes (--) will not be executed and considered as a comment.
- Line Comments beginning with a hash sign (#):
Line or Single-line comments start with a hash sign (#) and end with carriage returns or line breaks. Any text between the hash sign (#) and the end of the line is considered as a comment.
Example:
--This illustrates single-line comments.
SELECT *
FROM Student #Student is the table/relational name
WHERE Marks>80; --This is filter condition
2) Block Comments
Block comments or Multi-line comments start with a forward-slash an asterisk (/*) sign and end with asterisk and forward-slash (*/) are considered as comments(multi-line). These comments can be placed anywhere, in, before or after big chunks of code. These comments can extend to more than 1 line.
Example:
/* This is an example of block or multi-line comments
Students table is used to retrieve data on
the basis of filtering condition*/
SELECT *
FROM Student
WHERE marks>80;
Note: There is no limit for comment (in case of block comments). Comments can be nested.