SQL Comments
A comment is a note inside a query that the engine ignores. Two dashes start a comment in SQL.
A note above a query
The comment explains the query below it. The engine skips the comment and runs the query as usual.
-- The first two customers by name
SELECT name FROM customers ORDER BY name LIMIT 2;A comment after code
A comment can follow a statement on the same line. The statement runs, and the note after the dashes is skipped.
SELECT name FROM customers WHERE customer_id = 3; -- the Toronto customerA condition switched off
The WHERE line sits behind dashes, so the filter is off. Every customer comes back, not only the ones in the USA.
SELECT name FROM customers
-- WHERE country = 'USA'
ORDER BY name;Example
-- The dearest product
SELECT name, price FROM products
ORDER BY price DESC
LIMIT 1; -- keep only the top rowKey points
- Comment: a note inside a query that the engine skips
- Two dashes start a comment.
- A comment runs to the end of its line.
- A comment can sit on its own line or after a statement on the same line.
- Commenting out a condition switches it off without deleting it.
In this example, the first comment names what the query looks for. The query sorts the products by price, dearest first. LIMIT keeps one row, and the end comment explains that cap.
Note
A comment changes nothing in the result, so a commented query returns the same rows as the plain query.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DB Fiddle
Exercise
What do the two dashes do in a query?