SQL Comments | SQL Tutorial - Learn with VOKS
Back Next

SQL Comments


๐Ÿ”น 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:

  • What the query does
  • Why something was written a certain way
  • Important reminders
  • Temporary disabling of code

๐Ÿ”น Why Are SQL Comments Important?

Comments help:

  • Make code easier to read
  • Help other developers understand your work
  • Debug queries
  • Temporarily disable parts of code

๐Ÿ”น 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.

Example:

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;


Example Code:
-- 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;
SQL
Introduction What is a Database, Advantages of Database, and Database Tables Creating Databases, Tables, Constraints, and Keys Defining Data Types, Unique ID, Inserting Values, and Handling NULL SELECT, DISTINCT, TOP, LIMIT, UPDATE, DELETE, ORDER BY, WHERE, HAVING, AND, OR, NOT Aggregate function; Min, Max, Count, Avg, Sum Wildcards, IN, AS, LIKE, BETWEEN, and Aliases SQL Joins: Inner, Outer, Left, Right, Full, Cross Set Theory for SQL: Joins, UNION, INTERSECT, EXCEPT, GROUP BY SQL Arithmetic Operators SQL Bitwise Operators SQL Comparison Operator SQL View/Null Functions SQL Comments SQL Case
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL