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.
Syntax:
— 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.
Query:
SELECT * FROM customers;
-- This is a comment that explains
the purpose of the query.
Multi-Line Comments
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.
Syntax:
/* multi line comment
another comment */
SELECT * FROM Customers;
Let’s consider that we want to fetch order_date with the year 2022.
Query:
/*
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
In-line comments are an extension of multi-line comments, comments can be stated in between the statements and are enclosed in between ‘/*’ and ‘*/’.
Syntax:
SELECT * FROM /* Customers; */
Let’s Suppose we have on a table with name orders and we are fetching customer_name.
Query:
SELECT customer_name,
/* This column contains the name of
the customer / order_date /
This column contains the date the
order was placed */ FROM orders;
More Examples:
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;
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...