SQL: Comments

SQL supports single-line and multi-line comments.

1. Single-line Comments

Used to comment out a single line or part of a line.
There are two ways:

a) Using -- (Double Hyphen)

Everything after -- on that line is treated as a comment.

-- This is a single-line comment
SELECT * FROM Customers; -- Fetch all customers

b) Using # (Hash)

Supported in MySQL (not standard SQL).

# This is a single-line comment in MySQL
SELECT * FROM Orders;


2. Multi-line (Block) Comments

Used to comment out multiple lines of code.

/* This is a multi-line comment
   It can span across multiple lines
   Useful for temporarily disabling code
*/
SELECT * FROM Products;

You can also use it inline:

SELECT /* all columns */ * FROM Employees;


3. Best Practices for SQL Comments

  • Use comments to explain why a query is written in a certain way, not just what it does.

  • Avoid over-commenting obvious statements.

  • Keep comments up to date with code changes.

  • Use block comments for large explanations or documentation.


Example combining all types
:

-- Get all customers from USA
SELECT * FROM Customers
WHERE Country = 'USA'; /* Filtering by country */

# MySQL style comment