-
Hajipur, Bihar, 844101
In MySQL, comments are notes or explanations added to SQL scripts, queries, or procedures to improve readability and maintainability. Comments do not affect query execution, meaning MySQL ignores them during runtime.
Comments are essential when working on large databases, collaborative projects, or complex queries, as they help developers understand the purpose of each query, calculation, or data manipulation step. Proper commenting also serves as documentation, aiding future maintenance and debugging.
MySQL supports three main types of comments:
Single-line comments using --
Single-line comments using #
Multi-line comments using /* */
Each type has its use case depending on length, placement, and readability.
--Single-line comments starting with two hyphens (--) are widely used for short notes. They require a space after the hyphens to be recognized properly.
-- This is a single-line comment
SELECT * FROM students;
-- Retrieve all students from the students table
SELECT id, name, age FROM students;
The comment explains the query’s purpose.
Helps other developers understand why this query exists without reading every line of SQL.
--Use for short, concise notes.
Keep the explanation focused on why, not what the query does.
Place comments above the query or at the end of a line for clarity.
#Another way to add a single-line comment is with the hash symbol (#). Everything after # on the same line is ignored.
# This is also a single-line comment
SELECT * FROM students;
SELECT id, name, age FROM students; # Fetching all student details
Functionally identical to --.
Often used in scripts migrated from other SQL systems where # is more familiar.
Use consistently throughout your project.
Avoid mixing -- and # in the same script unless needed.
/* */Multi-line comments are enclosed between /* and */. They can span multiple lines and are particularly useful for detailed explanations, large blocks of code, or temporarily disabling multiple queries.
/* This is a multi-line comment
It can span several lines
and MySQL will ignore it
*/
SELECT * FROM students;
/*
This query retrieves all students
who have enrolled in at least one course.
It joins the students and enrollments tables
and filters only active enrollments.
*/
SELECT s.name, e.course_id
FROM students s
JOIN enrollments e ON s.id = e.student_id
WHERE e.status = 'Active';
Helps provide context for complex queries.
Useful in team environments for documenting logic and purpose.
Comments can temporarily disable parts of a query to test or debug SQL scripts without removing code.
SELECT id, name, age
FROM students
-- WHERE age > 18
ORDER BY name;
The WHERE clause is ignored during execution.
This technique is useful when testing queries incrementally.
SELECT id, name, age -- Fetching student details only
FROM students;
Inline comments help explain specific columns or calculations without adding extra lines.
Comments are essential in procedures, triggers, and functions to describe logic, parameters, and expected outcomes.
DELIMITER //
CREATE PROCEDURE GetActiveStudents()
BEGIN
-- Retrieve students who have active enrollments
SELECT s.name, e.course_id
FROM students s
JOIN enrollments e ON s.id = e.student_id
WHERE e.status = 'Active';
END //
DELIMITER ;
Comments inside procedures clarify complex joins, conditions, and business logic.
They reduce time spent understanding or debugging the procedure later.
SQL scripts often include step-by-step instructions using comments:
-- Step 1: Create students table
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
-- Step 2: Insert sample data
INSERT INTO students (id, name, age)
VALUES (1, 'Aisha', 20), (2, 'Nina', 22);
-- Step 3: Retrieve all students
SELECT * FROM students;
Makes the script self-explanatory.
Serves as documentation for database initialization or migration.
Explain the why, not the what:
Instead of -- Select all students, explain the purpose:-- Retrieve students with active enrollment for reporting
Be concise but clear:
Avoid overly long comments unless necessary.
Use multi-line comments for blocks:
Especially for complex joins or calculations.
Keep comments updated:
Outdated comments can confuse developers.
Use inline comments judiciously:
Explain specific columns or expressions, but avoid clutter.
Consistency is key:
Choose one style (--, #, or /* */) and use it consistently across scripts.
Disabling multiple queries during testing:
/*
INSERT INTO students VALUES (3, 'Leena', 23);
INSERT INTO students VALUES (4, 'Riya', 24);
*/
Explaining joins and aggregations:
/*
Join students and enrollments
Calculate number of active courses per student
*/
SELECT s.name, COUNT(e.course_id) AS ActiveCourses
FROM students s
LEFT JOIN enrollments e ON s.id = e.student_id
WHERE e.status = 'Active'
GROUP BY s.id;
Documenting procedures and triggers for team collaboration.
MySQL supports single-line (--, #) and multi-line (/* */) comments.
Comments do not affect execution and are ignored by MySQL.
Use comments to explain the purpose, logic, or parameters of queries, scripts, and procedures.
Comments are essential for debugging, team collaboration, and long-term maintenance.
Best practices include explaining the why, keeping comments concise and updated, and maintaining consistency.
Effective use of comments ensures SQL scripts are readable, maintainable, and easier to debug, especially in collaborative or complex database projects.
Add a single-line comment using -- to describe a query that retrieves all students.
Add a single-line comment using # at the end of a SELECT statement explaining the purpose.
Write a multi-line comment (/* */) above a query joining students and enrollments explaining the logic.
Use a comment to temporarily disable a WHERE clause in a query.
Add an inline comment explaining a calculated column in a SELECT statement.
Write a comment inside a stored procedure describing the purpose of a JOIN.
Add step-by-step comments in a script for creating a table, inserting data, and retrieving data.
Use a comment to explain a GROUP BY query counting active courses per student.
Add a multi-line comment to temporarily disable multiple INSERT statements in a script.
Add a comment explaining why a NULLIF function is used in a query for handling zero scores.