Single line and Multi line comment in PostgreSQL

In PostgreSQL, you can add comments to your SQL code to make it more understandable and maintainable. There are two types of comments you can use: single-line comments and multi-line comments.  As the name suggests multi-line comments will span across multiple lines. Let see how to insert a single line and multi line comment in PostgreSQL with example for each.

 

Single line and Multi line comment in PostgreSQL:

Single Line comment in PostgreSQL:

The syntax for creating a single line comment in PostgreSQL is — symbol. Single-line comments start with — and continue to the end of the line. Everything after — on that line is considered a comment and is ignored by the SQL parser.

 

-- comment goes here

 

Example – Single line comment in PostgreSQL

Example 1:

You can create a SQL comment on a single line in PostgreSQL as shown below

 

-- selecting everything from Table1

<em>Select * from Table1;</em>

Example 2:

 

-- This is a single-line comment

SELECT * FROM employees;   -- This comment is also single-line

 

 

 

Multiline comment in PostgreSQL:

The syntax for creating a multi line comment in PostgreSQL starts with /* and ends with */ symbol. They can span multiple lines.

 

/* comment goes here */


Example – Multi line comment in PostgreSQL:

You can create a SQL comment on multiple lines in PostgreSQL as shown below

 

/* selecting everything from Table1

Author: postgresqldatascience.com */

Select * from Table1;

 

 

Summary:

Here is a practical example combining both single-line and multi-line comments in PostgreSQL

 

-- Retrieve all records from the employees table

SELECT * FROM employees;  -- This selects all columns and rows


/*

The following query retrieves all records from the departments table.

We use this to get a list of all departments in the company.

*/

SELECT * FROM departments;


/*

Example ends

*/

Using comments effectively can help make your SQL scripts easier to read and understand, especially for others who may work with your code in the future.

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts