SQL Comments
SQL comments are used to add explanatory notes to your code or to temporarily disable parts of your SQL statements. They're essential for making your code easier to understand and maintain.
Single-Line Comments
Single-line comments begin with --
and continue to the end of the line. Any text after --
on that line is ignored by the SQL interpreter.
Examples of Single-Line Comments
Explanatory Comment
-- Select all columns from the Customers table:
SELECT * FROM Customers;
Output
(All rows and columns from the Customers table will be displayed.)
Comment at the end of a line
SELECT * FROM Customers -- WHERE City='Berlin';
Output
(All rows and columns from the Customers table will be displayed.)
Commenting out a line
-- SELECT * FROM Customers;
SELECT * FROM Products;
Output
(All rows and columns from the Products table will be displayed.)
Multi-Line Comments
Multi-line comments start with /*
and end with */
. Everything between these markers is ignored.
Examples of Multi-Line Comments
Explanatory Multi-line Comment
/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;
Output
(All rows and columns from the Customers table will be displayed.)
Commenting out Multiple Lines
/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM Categories;*/
SELECT * FROM Suppliers;
Output
(All rows and columns from the Suppliers table will be displayed.)
Commenting out Part of a Line or Statement
You can use multi-line comments to selectively comment out parts of a line or statement. This is useful for debugging or temporarily disabling specific parts of your SQL code.
Commenting out part of a line
SELECT CustomerName, /*City,*/ Country FROM Customers;
Output
CustomerName | Country
----------------------
(Customer names and countries will be displayed, City will be omitted)
Commenting out part of a statement
SELECT * FROM Customers WHERE (CustomerName LIKE 'L%'
OR CustomerName LIKE 'R%' /*OR CustomerName LIKE 'S%'
OR CustomerName LIKE 'T%'*/ OR CustomerName LIKE 'W%')
AND Country='USA'
ORDER BY CustomerName;
Output
(Customers whose names start with 'L', 'W' or 'R' and are from the USA will be returned, sorted by name.)