Practical SQL

Comments in SQL

Constantin LunguUpdated 1 min read

Photo by Raphael Schaller on Unsplash

Let's look at the available options in terms of SQL comments in BigQuery.

If you ever need to pass on a note to the future you or a fellow developers about the reasoning behind a particular approach or something to watch out for in a query, you can write a comment.

Based on where we put them, we can distinguish between:
- single line, taking the entire line
- in-line, comments intertwined with code
- multi-line, comments spanning across lines

With regards to the notation, we have:
- the '--' will turn everything after it on the same line into a comment
- the '#' which you might recognize from say Python, and works like '--'
- the '/* */' block which clearly marks it start and end, making possible the insertions of a comment inside a code line and multi-line comments

How much are you using comments in your SQL code and in what situations?

BigQuery SQL on an orderline_updates CTE demonstrating comment styles: a full-line -- comment, inline -- and # comments after columns, an inline /* */ comment followed by AS total_quantity, and a multi-line /* */ block before FROM; the query returns four order lines normally.

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.