SQL Comments
Illustration representing SQL comments tutorial with single-line and multi-line comment examples
SQL comments allow you to add notes inside your SQL queries. These notes are ignored by the database engine but help developers understand the purpose of queries, logic, and complex conditions.
In this beginner-friendly SQL tutorial, you will learn:
What SQL comments are
Types of SQL comments
How to write comments in SQL
Best practices
Real-world examples
SQL comments are annotations or notes added inside SQL code to explain what the query does.
They are ignored during execution and help in:
Improving readability
Explaining complex logic
Documenting queries
Debugging and testing
SQL supports three types of comments:
Single-line comments using --
Single-line comments using # (MySQL only)
Multi-line comments using /* ... */
Let's understand each with examples.
--This is the most common type of comment.
-- Select all users
SELECT * FROM users;
SELECT name FROM users; -- Fetch only names
# (MySQL Only)This type of comment works only in MySQL.
# This is a MySQL comment
SELECT * FROM products;
/* ... */Used for large explanations.
/*
Fetch all employees
who belong to the IT department
and have salary above 50,000
*/
SELECT * FROM employees
WHERE department = 'IT'
AND salary > 50000;
SELECT /* get employee names */ name
FROM employees;
Comments help developers understand the purpose of a query.
Useful when queries involve joins or nested conditions.
You can comment out parts of queries temporarily.
Code becomes easier for others to maintain.
You can temporarily disable part of a query.
SELECT name, email
FROM users
/* WHERE status = 'active' */;
Here, the WHERE clause is ignored.
/*
Join orders with users to get
customer name for each order
*/
SELECT orders.id, users.name
FROM orders
JOIN users ON orders.user_id = users.id;
SELECT * FROM sales
WHERE amount > 1000 -- Only big orders
AND region = 'North';
# Delete inactive accounts
DELETE FROM accounts
WHERE is_active = 0;
✔ Use -- for short comments
✔ Use /* ... */ for long explanations
✔ Avoid excessive comments (keep them meaningful)
✔ Update comments if query changes
✔ Use comments to describe complex logic or business rules
✔ Never include sensitive information (passwords/API keys)
In this SQL Comments tutorial, you learned:
What SQL comments are
How to use single-line and multi-line comments
MySQL-only comment syntax (#)
Real-world examples
Best practices
SQL comments make your queries more readable, professional, and easier to maintain.