Skip to content

SQL - Comments

A comment is a piece of text within a program or script that is ignored by the compiler or interpreter. In SQL, comments serve two main purposes: to explain complex sections of code for human readers, and to temporarily disable parts of a statement during debugging.

A single-line comment starts with two consecutive hyphens (--) and extends to the end of the line. Any text after the hyphens on that line will not be executed.

MySQL also supports the hash symbol (#) for single-line comments.

-- This is a single-line comment.
-- Retrieve all active users who registered in the last 90 days
SELECT UserID, Email
FROM Users
WHERE IsActive = TRUE
AND CreatedAt >= CURRENT_DATE - INTERVAL '90 days';

Here, we can temporarily disable the ORDER BY clause to test the query’s behavior without sorting.

SELECT * FROM Customers
WHERE City = 'New York'
-- ORDER BY LastName ASC;

A multi-line comment is used to comment out a block of SQL code. It starts with /* and ends with */. All text between these delimiters will be ignored.

/*
This is a multi-line comment.
It can span several lines.
*/
/*
This query calculates the total sales for each product category.
It joins three tables:
- Categories: to get the category name.
- Products: to link categories and order items.
- OrderItems: to get the quantity and price for each sale.
*/
SELECT
c.CategoryName,
SUM(oi.Quantity * oi.UnitPrice) AS TotalSales
FROM Categories c
JOIN Products p ON c.CategoryID = p.CategoryID
JOIN OrderItems oi ON p.ProductID = oi.ProductID
GROUP BY c.CategoryName;

Multi-line comments are also useful for disabling a part of a WHERE clause during debugging.

SELECT * FROM Products
WHERE CategoryID = 1
/*
AND IsAvailable = TRUE
AND Price < 100.00
*/