Open In App
Related Articles

SQL | Comments

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

  1. Single-line comments
  2. Multi-line comments
  3. 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; 

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 review-team@geeksforgeeks.org. 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.

Last Updated : 18 Apr, 2023
Like Article
Save Article
Similar Reads