SQL Comments
In SQL, comments are used to include explanations or notes within your SQL code that are not executed as part of the SQL statements. Comments can make your SQL code easier to understand and maintain. There are two types of comments in SQL: single-line comments and multi-line comments.
Single-Line Comments
Single-line comments start with two hyphens (--
). Anything following --
on that line is considered a comment and will be ignored by the SQL engine.
Example:
SELECT * FROM Employees; -- This is a comment
Output:
+----+----------+-----------+--------+
| ID | Name | Position | Salary |
+----+----------+-----------+--------+
| 1 | John Doe | Manager | 60000 |
| 2 | Jane Doe | Developer | 55000 |
| 3 | Sam Smith| Analyst | 50000 |
+----+----------+-----------+--------+
In this example, -- This is a comment
is ignored by the SQL engine.
Multi-Line Comments
Multi-line comments start with /*
and end with */
. Anything between /*
and */
is considered a comment and will be ignored by the SQL engine. Multi-line comments can span multiple lines.
Example:
/*
This is a multi-line comment
It can span multiple lines
*/
SELECT * FROM Employees;
Output:
+----+----------+-----------+--------+
| ID | Name | Position | Salary |
+----+----------+-----------+--------+
| 1 | John Doe | Manager | 60000 |
| 2 | Jane Doe | Developer | 55000 |
| 3 | Sam Smith| Analyst | 50000 |
+----+----------+-----------+--------+
In this example, everything between /*
and */
is ignored by the SQL engine.
Example with Both Types of Comments
-- Fetch all records from the Employees table
SELECT * FROM Employees; -- Select statement
/*
We can also add comments in between SQL statements
to explain different parts of the query
*/
SELECT Name, Position /* Columns to select */
FROM Employees; /* From the Employees table */
Output:
+----+----------+-----------+--------+
| ID | Name | Position | Salary |
+----+----------+-----------+--------+
| 1 | John Doe | Manager | 60000 |
| 2 | Jane Doe | Developer | 55000 |
| 3 | Sam Smith| Analyst | 50000 |
+----+----------+-----------+--------+
+----------+-----------+
| Name | Position |
+----------+-----------+
| John Doe | Manager |
| Jane Doe | Developer |
| Sam Smith| Analyst |
+----------+-----------+
In these examples, the comments do not affect the execution of the SQL statements and are used solely for documentation purposes.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.