SQL Comments: The Complete Guide

1
539
How To Add Comments in SQL Query Tutorial With Example

Adding Comments in SQL queries is a thoughtful idea because it is used to explain the sections of SQL statements or prevent the execution of SQL statements. Comments are not supported in the Microsoft Access database.

Mozilla Firefox and Microsoft Edge use the Microsoft Access database in our examples.

How to Comment SQL Statements

To comment on the SQL statement, use the — symbol. Comments can appear on a single line or span across multiple lines.

SQL Comments can be written in the following formats:

  1. Single-line comments.
  2. Multi-line comments
  3. Inline comments

SQL Single-line comment

To comment on a single-line comment, use the — syntax.

-- comment goes here

Any text between — and the end of the line will be ignored (will not be executed).

The comment started with symbol must be at the end of the line in your SQL statement with the line break after it. This is because the above commenting method can only span a single line within your SQL and must be at the end of the line.

See the following example. To create a table, check out the article on creating a table in MySQL.

-- fetch the records staring from LV
SELECT * FROM Products
WHERE ProductName LIKE 'LV%'

See the following output.

How To Add Comments in SQL Query Tutorial

SQL Multi-line comments

To comment out multi-line SQL statements, use the /* */ symbols.

/*Select all the columns
of all the records
in the Products table:*/

SELECT * FROM Products

See the output.

Multi-line Comments

To ignore just the part of the statement, also use a /* */ comment.

See the following example, which uses a comment to ignore part of the line.

SELECT ProductName, /*ProductCategory,*/ ProductPrice
FROM Products

See the following output.

Add Comments in SQL
That’s it for commenting out SQL statements.

1 Comment

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.