SQL - Comments


In Programming, a comment is a technique to hide a line of code from the compiler. It is also defined as an annotation and program-readable explanation of a computer program, added with the purpose of making the source code easier for humans to understand in a better way.

SQL-Comments

Comments are used to explain the section of the SQL statement; or to stop the execution of the statement. So, whenever a line(s) of code is marked as a comment in a program, it is not executed.

Let us say we are performing a SQL query to display the contents of a table. But if the user does not understand the query, then we can utilize the comments to explain the idea or the query.

There are three types of comments in SQL, let’s see them in detail −

  • Single-line comment.
  • Multi-line comment.
  • In line comment.

Single-line comment

A single line comment is one that ends in a single line and begins with '—'(double hyphen), and the text after the '—'(double hyphen) is not executable.

Syntax

Following is the syntax of the single-line comment.

--fetch all the table details
--another comment.
--SQL SELECT Query.
SELECT * from table_name

Multi-line comment

A multi-line comment is a text that starts in one line and ends in different line, as well as text which is between these (/*…. */) symbols is known as multiline comment.

Syntax

Following is the syntax of the multi-line comment.

/* this is first comment
This is second comment
Multi line comment*/
SELECT * FROM table_name;

Inline-comment

An inline comment is the same as a multiline comment that is enclosed between /* and /*; but, an inline comment is one that is present at the same line where the SQL query is provided.

Syntax

Following is then syntax of the inline comment.

SELECT * FROM table_name; /*customers*/

Example

In the following example, we define a SQL query that will create a table and display its contents, as well as provide all types of comments to explain the query in a better way.

--CREATING A TABLE
CREATE TABLE Customerss(ID INT NOT NULL, NAME VARCHAR(50), AGE INT NOT NULL);
/* INSERTING COLUMN'S VALUE 
IN A TABLE NAMELY CUSTMERS */
INSERT INTO Customerss VALUES(01, 'Tutorialspoint', 10);
INSERT INTO Customerss VALUES(02, 'Tutorix', 03);
SELECT * FROM Customerss; /* DISPLAYING TABLE DETAILS */

When we run the above SQL query, we get the column values, however the comment line is not executable, as we can see in the table below −

+----+----------------+-----+
| ID | NAME           | AGE |
+----+----------------+-----+
|  1 | Tutorialspoint |  10 |
|  2 | Tutorix        |   3 |
+----+----------------+-----+
Advertisements