Coding Ref

SQL Comments

SQL Comments

In SQL, comments are used to add explanatory notes or documentation to a query. They are ignored by the database engine when the query is executed, and are only used to provide information for humans who read the code.

Two types of comments

There are two types of comments in SQL:

Single-line comments

These comments start with a double dash (--) and continue until the end of the line. Anything on the same line after the double dash is considered a comment and will be ignored by the database engine.

For example:

SELECT *
FROM Customer
-- This is a single-line comment
WHERE City = 'New York'

In this example, the text after the double dash (--) is a single-line comment that explains what the WHERE clause does. The database engine will ignore this comment and only execute the SELECT and WHERE clauses.

Block comments

These comments start with a /* symbol and end with a */ symbol. Anything between these symbols is considered a comment and will be ignored by the database engine.

For example:

SELECT *
FROM Customer
/* This is a block comment.
It can span multiple lines and will be ignored by the database engine. */
WHERE City = 'New York'

In this example, the text between the /* and */ symbols is a block comment that explains what the WHERE clause does. The database engine will ignore this comment and only execute the SELECT and WHERE clauses.

Conclusion

In SQL, comments are used to add explanatory notes or documentation to a query. They are ignored by the database engine when the query is executed, and are only used to provide information for humans who read the code.

You'll also like

Related tutorials curated for you

    How to fix the 'Ambiguous Column Name' error in SQL

    SQL aliases

    How to get the day of the week in SQL

    What is the AS statement in SQL?

    What is SQL ANY?

    What is the unique constraint in SQL?

    Pandas read SQL

    How to combine two columns in SQL

    What is CAST function in SQL?

    What is a blind SQL injection?

    What is SQL ALL?

    Find column names in SQL