Comments are used to explain sections of SQL statements or to prevent SQL statements from being executed. As is any programming language comments matter a lot in SQL also. In this set, we will learn about writing comments in any SQL snippet.
Comments can be written in the following three formats:
- Single-line comments
- Multi-line comments
- In-line comments
Single Line Comments
Comments starting and ending in a single line are considered single-line comments. A line starting with ‘–‘ is a comment and will not be executed.
— single line comment
— another comment
SELECT * FROM Customers;
Let’s see an example in which we have one table name Customers in which we are fetching all the data records.
SELECT * FROM customers;
-- This is a comment that explains
the purpose of the query.
Comments starting in one line and ending in different lines are considered as multi-line comments.
A line starting with ‘/*’ is considered as starting point of the comment and is terminated when ‘*/’ is encountered.
/* multi line comment
another comment */
SELECT * FROM Customers;
Let’s consider that we want to fetch order_date with the year 2022.
This is a multi-line comment that explains
the purpose of the query below.
The query selects all the orders from the orders
table that were placed in the year 2022.
SELECT * FROM orders WHERE YEAR(order_date) = 2022;
In-line comments are an extension of multi-line comments, comments can be stated in between the statements and are enclosed in between ‘/*’ and ‘*/’.
SELECT * FROM /* Customers; */
Let’s Suppose we have on a table with name orders and we are fetching customer_name.
/* This column contains the name of
the customer / order_date /
This column contains the date the
order was placed */ FROM orders;
There are a few more examples of Multiline comments and inline comments.
Multi line comment ->
/* SELECT * FROM Students;
SELECT * FROM STUDENT_DETAILS;
SELECT * FROM Orders; */
SELECT * FROM Articles;
In line comment ->
SELECT * FROM Students;
SELECT * FROM /* STUDENT_DETAILS;
SELECT * FROM Orders;
SELECT * FROM */ Articles;
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.