← Back to MySQL Course | Chapter 1: Introduction & Basics | Lesson 8 of 8

MySQL Comments

A comment in SQL is like a sticky note in a book margin that the computer ignores. It is only there so people can understand what the code is doing.
Syntax
sql
-- single-line comment
# MySQL-style single-line comment
/* multi-line
   comment */

Single-Line Comments with Double Dash

A double-dash (--) starts a comment that runs to the end of the line, but standard SQL requires a space immediately after the dashes or MySQL may not recognize it as a comment at all.

Example: Single-Line Comments with Double Dash

sql
-- This is a comment
SELECT 1;

Single-Line Comments with Hash

The hash symbol (#) is a MySQL-specific single-line comment marker that behaves like the double-dash but, unlike it, doesn't require a trailing space — a small but easy-to-forget difference between the two styles.

Example: Single-Line Comments with Hash

sql
#This is a MySQL-specific comment
SELECT 1;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Multi-Line Comments

Block comments start with /* and end with */, and can span multiple lines, which makes them the right choice for longer explanations of why a complex query is structured the way it is.

Example: Multi-Line Comments

sql
/* This is a
   multi-line comment */
SELECT 1;

Comments inside SQL Statements

You can drop a block comment mid-statement, right next to a column name or clause, to document exactly what that piece does without breaking the SQL syntax around it.

Example: Comments inside SQL Statements

sql
SELECT name /* the customer's full name */ FROM customers;

Executable Comments

MySQL supports a special executable comment syntax (/*! ... */) that other database systems simply ignore as a normal comment, letting you write MySQL-specific optimizations that stay portable to other engines.

Example: Executable Comments

sql
SELECT /*! SQL_NO_CACHE */ * FROM customers;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing --comment without a space after the two dashes, when MySQL requires -- with a space (or a control character) to start a comment.
  2. Forgetting the closing */ on a block comment, so the rest of the script is commented out.
  3. Putting a comment before a ; in a way that hides part of the statement, like SELECT 1 -- ;.
Chapter Summary
  • MySQL is a relational database management system that stores data in tables and uses SQL to work with it.
  • Installing MySQL and using Workbench or the command line lets you run SQL statements.
  • Comments and an overview of data types are the first building blocks for writing MySQL code.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.