๐น What Are SQL Comments?
A comment in SQL is text written inside your SQL code that the database ignores when executing the query.
Think of comments as:
๐ Notes for humans (developers)
โ Ignored by the database
They help explain:
๐น Why Are SQL Comments Important?
Comments help:
๐น Types of SQL Comments
There are two main types of SQL comments:
| Comment Type | Symbol Used | Purpose | |---------------------|-------------|----------| | Single-line comment | `--` | Comment one line | | Multi-line comment | `/* */` | Comment multiple lines |
๐น 1๏ธโฃ Single-Line Comments (--)
Single-line comments start with:
--
Everything after -- on that line is ignored.
Example:
-- This query selects all employees SELECT * FROM Employees;
You can also write it at the end of a line:
SELECT * FROM Employees; -- Get all employee records
๐น 2๏ธโฃ Multi-Line Comments (/* */)
Multi-line comments start with:
/*
and end with:
*/
Everything inside is ignored.
Example:
/* This query retrieves employees from the HR department */ SELECT * FROM Employees WHERE department = 'HR';
๐น Example Table (Markdown Format)
Hereโs a sample Employees table:
| id | name | department | salary | |----|---------|------------|--------| | 1 | Alice | HR | 50000 | | 2 | Bob | Sales | 60000 | | 3 | Charlie | IT | 55000 | | 4 | David | HR | 45000 |
๐น Commenting Complex Queries
Comments are especially helpful in long queries.
Example:
-- Get high-paying HR employees SELECT id, name, salary FROM Employees WHERE department = 'HR' AND salary > 48000;
๐น Using Comments to Disable Code (Very Useful!)
You can temporarily disable part of a query.
SELECT id, name, salary FROM Employees WHERE department = 'HR' -- AND salary > 48000;
The salary condition is now ignored.
Or disable multiple lines:
SELECT id, name, salary FROM Employees /* WHERE department = 'HR' AND salary > 48000 */ ;
๐น Best Practices for SQL Comments
| Best Practice | Explanation | |---------------|-------------| | Explain WHY, not WHAT | Avoid obvious comments like "Select all columns" | | Keep comments clear | Short and meaningful | | Update comments | Make sure comments match your code | | Use for complex logic | Especially in joins and conditions |
๐น Real-World Example
/* Monthly HR Salary Report Author: Admin Purpose: Retrieve HR employees earning above 45,000 */ SELECT id, name, salary FROM Employees WHERE department = 'HR' AND salary > 45000;
-- This query selects all employees
SELECT * FROM Employees;
SELECT * FROM Employees; -- Get all employee records
/*
This query retrieves employees
from the HR department
*/
SELECT *
FROM Employees
WHERE department = 'HR';
-- Get high-paying HR employees
SELECT id, name, salary
FROM Employees
WHERE department = 'HR'
AND salary > 48000;
-- Disabling part of the condition
SELECT id, name, salary
FROM Employees
WHERE department = 'HR'
-- AND salary > 48000;
-- Disable multiple lines
SELECT id, name, salary
FROM Employees
/*
WHERE department = 'HR'
AND salary > 48000
*/
;
/*
Monthly HR Salary Report
Author: Admin
Purpose: Retrieve HR employees earning above 45,000
*/
SELECT id, name, salary
FROM Employees
WHERE department = 'HR'
AND salary > 45000;