×

How to Add Comments in SQL?

How to Add Comments in SQL

Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections and to provide appropriate documentation to a program. In addition, it also prevents the code from being executed.

Comments can be added in SQL in the following ways:

  1. Single Line Comment
  2. Multi Line Comment
  3. Inline Comment

1. Single Line Comments: Comments which are added to a single line are called as single line comments.

Syntax:

--Add a single line which is to be commented

Example:

SELECT * FROM employee;--Display all the records from employee table

Output:

 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Nikita   |      30000 |
 |      2 | Riddhi   |      25000 |
 |      3 | Nayan    |      45000 |
 |      4 | Shruti   |      15000 |
 |      5 | Anurati  |      55000 |
 +--------+----------+------------+
 5 rows in set (0.00 sec) 
Add Comments in SQL

“Display all the records from employee table” is displayed as a single line comment in above example, and also not get executed.

2. Multi Line Comment: Comments which are added to multiple lines together are called as multi line comments.

Syntax:

 /*
 Add multiple lines
 which are to be commented
 */ 

Example:

 /* Select all the
 records which are
 present in employee table */
 SELECT *FROM employee; 

Output:

 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Nikita   |      30000 |
 |      2 | Riddhi   |      25000 |
 |      3 | Nayan    |      45000 |
 |      4 | Shruti   |      15000 |
 |      5 | Anurati  |      55000 |
 +--------+----------+------------+
 5 rows in set (0.00 sec) 
Add Comments in SQL

“Select all the records which are present in employee table” is displayed as a multiline comment in above example.

3. Inline Comment: Comments which are added in between the SQL statements are called as inline comments.

Syntax:

SQL Statements /*single or multiple lines which are to be commented*/ continuation of SQL Statements

Example:

SELECT Emp_ID, /*Emp_Name*/ Emp_Salary FROM employee;
  • Output:
 +--------+------------+
 | Emp_ID | Emp_Salary |
 +--------+------------+
 |      1 |      30000 |
 |      2 |      25000 |
 |      3 |      45000 |
 |      4 |      15000 |
 |      5 |      55000 |
 +--------+------------+
 5 rows in set (0.00 sec) 
Add Comments in SQL

“single or multiple lines which are to be commented” is displayed as an inline comment in above example.


Related Topics

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.