SQL Comments

In this tutorial, we'll learn about the comments in SQL and how to use them with examples.

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.


Comments With Statements

It's also possible to include comments within a line of SQL statements. For example,

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

Here, the comments are

  • -- selects all records
  • -- from the Students table

The above SQL statement is equivalent to

SELECT * 
FROM Students;

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, anything between /* and */ is a comment and is ignored by database management systems.


Comments Within Statements

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

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

The above SQL statement is equivalent to

SELECT *
FROM Students;

Using Comments to Debug Code

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

This helps us to test our SQL code without removing them completely. For example,

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

SELECT *
FROM Students;

Here, SQL will only fetch all records from the Students table.

Did you find this article helpful?