SQL Comments

11/22/2025
All Articles

Illustration representing SQL comments tutorial with single-line and multi-line comment examples

SQL Comments

SQL Comments – SQL Tutorial for Beginners

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


What Are SQL Comments?

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


Types of SQL Comments

SQL supports three types of comments:

  1. Single-line comments using --

  2. Single-line comments using # (MySQL only)

  3. Multi-line comments using /* ... */

Let's understand each with examples.


Single-Line Comments Using --

This is the most common type of comment.

Example:

-- Select all users
SELECT * FROM users;

Comment after a query:

SELECT name FROM users; -- Fetch only names

Single-Line Comments Using # (MySQL Only)

This type of comment works only in MySQL.

Example:

# This is a MySQL comment
SELECT * FROM products;

Multi-Line Comments Using /* ... */

Used for large explanations.

Example:

/*
  Fetch all employees
  who belong to the IT department
  and have salary above 50,000
*/
SELECT * FROM employees
WHERE department = 'IT'
AND salary > 50000;

Inline multi-line comments:

SELECT /* get employee names */ name
FROM employees;

Why Use Comments in SQL?

✔ Improve readability

Comments help developers understand the purpose of a query.

✔ Explain complex logic

Useful when queries involve joins or nested conditions.

✔ Make debugging easier

You can comment out parts of queries temporarily.

✔ Helpful for working in teams

Code becomes easier for others to maintain.


Commenting Out SQL Code (Debugging)

You can temporarily disable part of a query.

Example:

SELECT name, email
FROM users
/* WHERE status = 'active' */;

Here, the WHERE clause is ignored.


Real-World Examples

Example 1: Documenting a JOIN Query

/*
  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;

Example 2: Explaining a Condition

SELECT * FROM sales
WHERE amount > 1000  -- Only big orders
AND region = 'North';

Example 3: MySQL-only comment

# Delete inactive accounts
DELETE FROM accounts
WHERE is_active = 0;

Best Practices for Using Comments

✔ 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)


Summary

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.

Article