Skip to main content
Log inGet a demo
Back to SQL Dictionary
Basic statements and clauses

SQL Comments

What are SQL Comments?

SQL Comments are non-executable lines of text in SQL code that are used to provide explanations, notes, or documentation within your SQL statements. These comments are not processed by the database engine and are purely for human readability.

When you would use it

SQL Comments are useful in the following situations:

  1. Documentation: Comments help explain the purpose and functionality of SQL statements, making it easier for developers, DBAs, and other team members to understand and maintain the code.

  2. Debugging: You can temporarily comment out portions of SQL code to test different parts of a query without completely removing them, which can be helpful for debugging.

  3. Communication: Comments allow you to convey important information to other team members, such as explanations of complex logic or reminders for future modifications.

Syntax

SQL comments can be written in different ways depending on the database system. The two most common ways are:

  1. Single-Line Comments: To create a comment that spans a single line, you can use a double hyphen (--) in most SQL dialects. Everything to the right of the double hyphen is treated as a comment and is ignored by the database.

    -- This is a single-line comment
    SELECT * FROM employees;
    
  2. Multi-Line Comments: Some SQL databases also support multi-line comments enclosed in /* and */.

    /* This is a
    multi-line comment */
    SELECT * FROM departments;
    

Parameter values

SQL comments don't have specific parameters. They are purely for documenting and clarifying the SQL code.

Example query

Consider a sample SQL query that retrieves employees with specific criteria:

SELECT * FROM employees
WHERE salary > 50000
--AND department = 'IT'
AND hire_date >= '2022-01-01';

In this query, the line containing AND department = 'IT' is commented out.

Example table response

Assuming a simplified "employees" table with the following data:

employee_idemployee_namesalarydepartmenthire_date
1John Smith60000IT2022-03-15
2Jane Doe48000HR2021-07-22
3Mark Johnson55000IT2022-02-10

If you execute the query with the commented line, the result will include all employees with a salary greater than $50,000, regardless of their department or hire date.

Use cases

SQL comments are handy in various scenarios:

  1. Explanations: Documenting the purpose of a query or a specific part of it.

  2. Deactivating Code: Temporarily disabling portions of SQL code for debugging or testing without removing them entirely.

  3. Team Collaboration: Enhancing team collaboration by providing context and clarity in complex SQL scripts.

SQL languages this is available for

SQL comments are available in most SQL database systems and are a standard feature of the SQL language. This includes databases like MySQL, PostgreSQL, SQL Server, Oracle, and others. The syntax may vary slightly between different database systems, but the concept of commenting remains consistent. Always refer to your specific database system's documentation for any variations in comment syntax.

Related

SQL Aliases

SQL Equal To

SQL Not Equal To

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes