Comments in MySql
Comments in MySQL
Comments in MySQL are used to add explanations or disable parts of queries without executing them. MySQL supports three types of comments:
1. Single-Line Comments (-- or #)
- Used for short explanations or debugging.
- Everything after
--(double hyphen with a space) or#is ignored by MySQL.
Example: Using --
SELECT * FROM employees; -- This fetches all employees
✅ Note: There must be a space after -- (--⎵).
❌ --This is wrong (No space after --)
Example: Using #
SELECT * FROM employees; # This is also a valid comment
2. Multi-Line Comments (/* ... */)
- Used for long comments or disabling multiple lines of code.
Example: Commenting out a block
/* This query retrieves employees from the IT department only.*/SELECT * FROM employees WHERE department = 'IT';
Example: Disabling a part of a query
SELECT * FROM employees WHERE department = 'IT' /* AND salary > 50000 */ORDER BY employee_name;
✅ The part inside /* ... */ is ignored.
3. Nested Comments (MySQL-Specific)
- MySQL supports nested comments using
/*! ... */. - Used for hints or version-specific code.
Example: Executed only in MySQL
SELECT * FROM employees /*! WHERE salary > 50000 */;
✅ Runs as normal SQL in MySQL.
❌ Ignored in other SQL databases.
Best Practices
✅ Use -- for quick inline notes.
✅ Use /* ... */ for long explanations or disabling code.
✅ Avoid excessive commenting in production queries.