042 SQL Comments
The "SQL Comments" page on W3Schools explains how to use comments in SQL to clarify sections of code or to prevent execution of specific statements. Here are the main points:
-
Purpose of Comments:
- Comments are used to explain parts of SQL statements or to stop certain statements from being executed.
- Note that Microsoft Access databases do not support SQL comments.
-
Single-Line Comments:
- Begin with
--; everything after this on the same line is ignored by SQL. - Examples show how to use single-line comments to explain SQL code or ignore parts of a statement.
- Begin with
-
Multi-Line Comments:
- Begin with
/*and end with*/. - Everything between these markers is ignored.
- Useful for larger explanations, to comment out multiple statements, or to skip parts of a line or statement.
- Begin with
-
Examples:
- The page provides practical examples demonstrating both single-line and multi-line comments in SQL code.
-
Key Point Exercise:
- The primary purpose of comments is to explain parts of SQL code or prevent execution of certain SQL statements.[1]
This page offers clear guidance and interactive examples for adding and using comments effectively in SQL.
SQL Comments
Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.
Note: Comments are not supported in Microsoft Access databases!
Single Line Comments
Single line comments start with --.
Any text between -- and the end of the line will be ignored (will not be executed).
The following example uses a single-line comment as an explanation:
-- Select all:
SELECT * FROM Customers;
The following example uses a single-line comment to ignore the end of a line:
SELECT * FROM Customers -- WHERE City='Berlin';
The following example uses a single-line comment to ignore a statement:
-- SELECT * FROM Customers;
SELECT * FROM Products;
Multi-line Comments
Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored.
The following example uses a multi-line comment as an explanation:
/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;
The following example uses a multi-line comment to ignore many statements:
/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM Categories;*/
SELECT * FROM Suppliers;
To ignore just a part of a statement, also use the/* */comment.
The following example uses a comment to ignore part of a line:
SELECT CustomerName, /*City,*/ Country FROM Customers;
The following example uses a comment to ignore 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;