COMMENTS

Finally, we look at comments. Commenting is a way to provide instructions to your query on what to and not to execute. This feature allows us to write good documentation on what is the query does and also useful for quickly testing and validation queries for example by commenting on a specific filter and reviewing results.

In SQL, we have multi-line comments and in-line comments both of which are handled differently.

Multiline Comments: /* */

We use the /* */ syntax to instruct the querying engine not to execute any instructions contained between the symbol. For example, the query below will ignore what is within the commenting syntax and only execute on the uncommenting commands.

/*
This query returns information
about customers from customers table
*/

select customer_id AS Id,
       first_name AS Name,
       last_name AS SurName
from customers
limit 5
| Id   | Name  | SurName  |
| ---- | ----- | -------- |
| 2001 | Alice | Johnson  |
| 2002 | Bob   | Williams |
| 2003 | Carol | Brown    |
| 2004 | David | Davis    |
| 2005 | Emma  | Miller   |

The running of the query will not be affected by the comments we have placed above. As you can see, the text is also muted/colored differently as well. This is very useful for documentation on what the query does.

Inline Comments

In-line comments do the same thing except it is restricted to only commenting one line at a time. Therefore, if we would like to achieve the same results using the in-line comment, we would modify our query to look like below.

--This query returns information
--about customers from customers table

select customer_id AS Id,
       first_name AS Name,
       last_name AS SurName
from customers
--where first_name != 'Bob'
limit 8
| Id   | Name  | SurName  |
| ---- | ----- | -------- |
| 2001 | Alice | Johnson  |
| 2002 | Bob   | Williams |
| 2003 | Carol | Brown    |
| 2004 | David | Davis    |
| 2005 | Emma  | Miller   |
| 2006 | Frank | Wilson   |
| 2007 | Grace | Moore    |
| 2008 | Henry | Taylor   |

Notice that in this example, we also commented on a filter --where first_name != 'Bob'. It means that at execution time, that filter will be ignored and all results will be returned.

This concludes our discussion of the foundations of SQL. TO continue your learning journey, navigate to the SQL intermediate section to build on what you have learned so far.