How to Comment in SQL

Do you need to make a note in your SQL code? Or maybe you want to add a helpful explanation for someone else who might be reading your code? SQL has a handy way of adding comments that can help make your code more readable and understandable: the -- syntax.

This syntax allows you to add a comment after any statement in your SQL code. For example, if you wanted to add a comment to the end of a SELECT statement, you would use the following syntax:

SELECT * FROM table_name -- This is a comment

Any text after the -- will be treated as a comment and will not be executed as part of the query.

Examples

Let's look at a few examples of how this syntax can be used. Suppose you have a query like this:

SELECT name, age FROM users WHERE age > 25;

If you wanted to add a comment to explain what this query is doing, you could use the following syntax:

SELECT name, age FROM users WHERE age > 25; -- Get the names and ages of users over 25

Additional Info About Commenting in SQL

The -- syntax is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database.

Want to build your own LLM Apps with AirOps👇👇