Skip to content

SQL Comments

A comment is text the database ignores. Comments explain what a query is for, and they are also a quick way to switch part of a query off while you work on it. SQL has two kinds.

The examples use the Productions table from the example database.

Two hyphens start a comment that runs to the end of the line:

-- Productions made in the UK
SELECT Title, Country
FROM Productions
WHERE Country = 'UK';

A -- comment can also follow code on the same line, which is handy for explaining a single column or condition:

SELECT Title, -- the production's name
Country
FROM Productions;

MySQL needs a space after the two hyphens, so write -- note rather than --note.

/* starts a comment and */ ends it. Everything between is ignored, however many lines it spans:

/*
Budget review: list every production with its budget,
largest first, for the quarterly report.
*/
SELECT Title, Budget
FROM Productions
ORDER BY Budget DESC;

Because a block comment can start and end anywhere, it can switch off part of a statement and leave the rest running. Here the Format column and one of the conditions are switched off:

SELECT Title, /* Format, */ Country
FROM Productions
WHERE Country = 'UK' /* AND Budget > 10000000 */;
Title Country
The Lantern Keepers UK
Quarry Lane UK
Six Bells UK