Beginner

24. SQL Comments

Learn how comments can be added to SQL code to explain queries and make them easier to understand.

📘 Lesson 24 of 728 ⏱ 6 min 🎯 Beginner

SQL comments are notes written inside SQL code that are ignored by the database engine when the query is executed. Single-line comments are commonly written using two hyphens, while multi-line comments can be written using /* and */ in database systems that support this syntax.

Well-written comments make SQL code easier to understand, maintain, review, and troubleshoot. This becomes especially important when queries are shared between analysts, developers, and database teams.

An analyst may add a comment above a query explaining that the query calculates monthly sales for a specific business region.

SQL
-- Get employees from Finance
SELECT
    EmployeeName,
    Salary
FROM Employees
WHERE Department = 'Finance';

Comments are useful in reporting queries, analytical scripts, stored procedures, data pipelines, testing, documentation, and team-based development.

  • Comments explain SQL code.
  • Comments are ignored during query execution.
  • -- is commonly used for single-line comments.
  • /* */ can be used for multi-line comments.
  • Comments improve SQL readability and maintenance.
Why are comments used in SQL?

Comments are used to explain SQL code and make queries easier to understand and maintain.

How do you write a single-line SQL comment?

A single-line comment can commonly be written using two hyphens (--).

Have you finished this lesson?

Introduction to SQL – All Lessons