Latest Code Tutorials

How To Add Comments in SQL Query Example


Add Comments in SQL queries are thoughtful idea because it is used to explain the sections of SQL statements, or to prevent the execution of SQL statements. Comments are not supported in the Microsoft Access databases. Mozilla Firefox and Microsoft Edge are using the Microsoft Access database in our examples.

How To Add Comments in SQL

In SQL, you can comment on the code just like any other language. Comments can appear on a single line or span across multiple lines. Let’s explore how to comment on your SQL statements.

Comments can be written in the following formats:

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

#Syntax Using — Symbol

The syntax for creating the comment in SQL using — symbol is the following.

-- 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. The above method of commenting can only span the single line within your SQL and must be at the end of the line.

See the following example. If you do not know how to create table, then check out the article on how to create 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

#Multi-line Comments OR Syntax Using /* and */ symbols

See the following query.

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

SELECT * FROM Products

See the output.

Multi-line Comments

If we want 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
Finally,  How To Add Comments in SQL Query Example is over.

Leave A Reply

Your email address will not be published.

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