Summary -
In this topic, we described about the below sections -
What is SQL Comment?
SQL Comments are used in SQL Statements to understand others easily what that statement purpose, how it works on the database. The one who writes the query understood easily, because he had an idea what data he wants to fetch from database, but others face a difficulty in understanding. Due to some complex queries like nested queries, joins it became complex to understand the query. So, we can explain the query in comments with little information for understanding easily.
In other side, comment also used to skip the execution of some part of the statement.
SQL Comment types
SQL Comments will not affect the SQL query execution. SQL allows two types comments. They are as follows -
- Single Line Comments
- Multi-line Comments
Single Line Comments
To write single line comments, Double hyphens (--) are used the beginning of the comment. It contains single row of information. The text should not go to the newline. The text after the double hyphens(--) considered s comment and won't get executed. The single line comment always start with '--'.
Example-
Consider an employee table with 9 employee's information like eid, ename, destination, etc,.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-11-07 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-11-07 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-11-07 | 25000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-11-07 | 24000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-10-08 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 20000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-10-08 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-10-08 | 15000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 15000.00 | 2000 |
Let us assume, we are trying to retrieve employee name, designation, and salary details from employee table. The query for the same as follows -
-- Single line comment
-- Query to fetch employee name, designation, and salary from table
SELECT ename, designation, salary FROM employee_details;
-- The above query can also written like below -
SELECT ename, -- Employee Name
designation, -- Employee designation
salary -- Employee Salary
FROM employee_details;
Output-
emp_name | designation | salary |
---|---|---|
Employee1 | Director | 45000.00 |
Employee2 | Director | 40000.00 |
Employee3 | Manager | 25000.00 |
Employee4 | Manager | 24000.00 |
Employee5 | Analyst | 20000.00 |
Employee6 | Analyst | 20000.00 |
Employee7 | Clerk | 15000.00 |
Employee8 | Salesman | 15000.00 |
Employee9 | Salesman | 15000.00 |
Explaining Example -
- In the above first query, comment line gives the information about what the statement used to do.
- In the second query, the comment specifies the column characteristics.
Multi-line Comments
Multi-row comments are used either to write multiple lines of information about statement or to ignore the multiple statements. The comments are beginning with slash followed by Asterix (/*) and end with Asterix followed by slash (*/).
Example -
Consider an employee table with 9 employee's information like eid, ename, destination, etc,.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-11-07 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-11-07 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-11-07 | 25000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-11-07 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-10-08 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 20000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-10-08 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-10-08 | 15000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 15000.00 | 2000 |
Let us assume, we are trying to retrieve employee name, designation, and salary details from employee table. The query for the same as follows -
/* Query to fetch employee name, designation,
Salary details from employee_details table */
SELECT ename, designation, salary FROM employee_details;
output-
emp_name | designation | salary |
---|---|---|
Employee1 | Director | 45000.00 |
Employee2 | Director | 40000.00 |
Employee3 | Manager | 25000.00 |
Employee4 | Manager | 24000.00 |
Employee5 | Analyst | 20000.00 |
Employee6 | Analyst | 20000.00 |
Employee7 | Clerk | 15000.00 |
Employee8 | Salesman | 15000.00 |
Employee9 | Salesman | 15000.00 |
Explaining Example -
- In the above example, multi-row comment line gives the information about what the statement used to do.