SQL-Comment


In this tutorial, we'll learn how to use comments with single line, multi-line, With Statements and comment to skip certain statements from execution in SQL.

SQL Comments

The purpose of making the source code easier for humans to understand used comment. Comments are brief descriptions of the logic and workings of the written Query in SQL. Comments are descriptions in the SQL code that help users better understand the intent and functionality of the SQL command. They are completely ignored by the database management systems.

Single Line Comments

In SQL, we use the double dash -- to write a single-line comment. The comment starts from the -- and ends with the end of line.

For example,

-- Fetching all records from the Students table 
SELECT * FROM Students;

Here, the comment is :

-- Fetching all records from the Students table

Database systems completely ignore this line from execution.

Multi-line Comments

In SQL, multi-line comments starts with /* and ends with */.

For example

/* selecting all records
from the
Students table */
SELECT *
FROM Students;

Here, the comment is :

anything between /* and */ is a comment and is ignored by database management systems.

Comments Within Statements

Similar to single-line comments, it is possible to include multi-line comments within a line of SQL statement. For example,

SELECT *
FROM /* table name here */ Students;

Here, the comment is :

/* table name here */

Using Comments to Debug Code

Suppose, we don't want to include certain SQL statements from execution. In such cases, instead of removing the statements, we can simply comment it out.

/* SELECT *
FROM Customers; */
-- the above statement is ignored by DBMS

SELECT *
FROM Students;

Next