SQL supports single-line and multi-line comments.
Used to comment out a single line or part of a line.
There are two ways:
--
(Double Hyphen)Everything after --
on that line is treated as a comment.
-- This is a single-line comment
SELECT * FROM Customers; -- Fetch all customers
#
(Hash)Supported in MySQL (not standard SQL).
# This is a single-line comment in MySQL
SELECT * FROM Orders;
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;
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