Comments

Published May 14, 2024
Contribute to Docs

In PostgreSQL, comments are annotations added to SQL code to provide information about the code without affecting its execution. They are primarily used for documentation purposes and to enhance code readability. Comments in PostgreSQL can be single-line or multi-line and are initiated with -- for single-line comments and, /* */ for multi-line comments.

Syntax

  • Single-line comment:
-- This is a single-line comment
  • Multi-line comment:
/*
This is a
multi-line comment
*/

Example

Here’s an example of how comments are used in PostgreSQL:

-- Selects employee IDs and their names from the employees table.
SELECT id, name
FROM employees;
/*
Selects all columns from the departments table.
This query retrieves detailed information about departments.
*/
SELECT *
FROM departments;

Note: In the above example, comments are used to describe what the following SQL statement does. The PostgreSQL compiler will ignore these comments when executing the SQL code.

All contributors

Looking to contribute?

Learn PostgreSQL on Codecademy