Dark Mode
Image

SQL Tutorial

SQL Database

SQL Injection

PL/SQL Tutorial

Sql Interview Question

SQl Quiz

SQL Comments

SQL Comments are used to explain the sections of the SQL statements, and used to prevent the statements of SQL. In many programming languages, comments matter a lot.

A Microsoft Access database does not support the comments. So, Mozilla Firefox and Microsoft Edge use the Microsoft Access database in the examples.

There are three types of comments, which are given below:

  1. Single line comments.
  2. Multi-line comments
  3. Inline comments

Single Line Comment

Comments starting and ending with a single line are said as individual line comments. The line which starts with '–' is a single line comment, and that particular line is not executed.

The text between -- and end of the line is ignored and cannot be executed.

Syntax:

  • -- single-line comment
  • -- another comment
  • SELECT * FROM Customers;

The following example uses a single-line comment:

Example 1

--Select all:  
SELECT * FROM Employees; 

The given example uses a single-line comment to ignore the end of the line:

Example 2

SELECT * FROM Customers -- WHERE City='London';  

The following example uses the single-line comment to ignore the statements:

Example 3

--SELECT * FROM Employees;  
SELECT * FROM Products;

Multi-line Comments

Comments that start in one line and end in different front are said as multi-line comments. The text between /* and */ is ignored in the code part.

The line starting with '/*' is considered as a starting point of comment and terminated when '*/' lies at the end.

Syntax:

 /* multi-line comment  
another comment */  
SELECT * FROM Customers;   

Example 1

/*Select all the columns  
of all the records  
in the Customers table:*/  
SELECT * FROM Employees; 

The below example uses a multi-line comment to ignore more statements:

Example 2

/*SELECT * FROM Customers;  
SELECT * FROM Products;  
SELECT * FROM Orders;  
SELECT * FROM Categories;*/  
SELECT * FROM Suppliers;  
 

 

To ignore some part of a statement, use the /*....... */ comment.

The following example uses a comment to ignore some part of any code:

Example of SQL Multi-line Comment:

/*SELECT * FROM Customers;  
SELECT * FROM Products;  
SELECT * FROM Orders;  
SELECT * FROM Categories;*/  
SELECT * FROM Suppliers;  

Example

SELECT CustomerName, /*City,*/ Country FROM Customers;  

The following example uses a comment to not to be the part of a statement:

Example

 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='America.'  
ORDER BY CustomerName; 

Inline comments:

Inline comments are an extension of multi-line comments, and comments can be stated between the statements and are enclosed in between '/*' and '*/.'

Syntax:

SELECT * FROM /*Employees; */

Examples:

Multi line comment ->  
/* SELECT * FROM Teachers;  
SELECT * FROM Teacher_DETAILS;  
SELECT * FROM Orders; */  
SELECT * FROM Course;   

 In line comment ->

SELECT * FROM Students;  
SELECT * FROM /* Employee_DETAILS;  
SELECT * FROM Orders;  
SELECT * FROM */ Topics;   

SQL Comment Indicators

SQL Comment Indicator is indicated according to the given examples

It includes the double hyphen ( — ), braces ( { } ), and C-style ( /* . . . */ ) comment delimiters. It also includes the comments after the statement.

SELECT * FROM customer; -- Selects all rows and columns  
SELECT * FROM employee; {Selects all rows and columns}  
SELECT * FROM employee; /*Selects all columns and rows*/copy to the clipboard  

 In the below examples, we place the comments on a single line code -

SELECT * FROM customer;  
-- Selects all the rows and columns  
SELECT * FROM employee;  
{Selects all columns and rows}  
SELECT * FROM customer;  
/*Selects all columns and rows*/ 

Examples of multi-line statements -

SELECT * FROM customer;  
-- Selects all columns and rows  
-- from the customer table  
SELECT * FROM customer;  
{Selects all columns and rows  
from the customer table}  
SELECT * FROM customer;  
/*Selects all columns and rows  
from the customer table*/copy to clipboard  
SELECT * -- Selects all columns and rows  
FROM customer; -- from the customer table  
SELECT * {Selects all columns and rows}  
FROM customer; {from the customer table}  
SELECT * /*Selects all columns and rows*/  
FROM customer; /*from the customer table*/copy to clipboard   

Comment / Reply From