SQL comments are used to explain and clarify the code, making it easier to understand for anyone reading it later. Comments are not executed as part of the SQL statement.
SQL Comments Syntax
In SQL, there are two types of comments: single-line and multi-line.
- Single-line comment: Begins with
--
- Multi-line comment: Enclosed between
/*
and*/
SQL Comments Example
SQL Comments Single line Comments
Single-line comments are useful for adding brief explanations or notes directly above a line of code. They are ideal for quick notes or reminders.
-- Selecting the first name and last name from the Patients table SELECT first_name, last_name FROM Patients;
SQL Comments Multi-line Comments
Multi-line comments allow you to provide more detailed explanations that span several lines. They can be helpful when you need to describe complex logic or provide context for a section of code.
/* The following query retrieves all doctors with their specialties from the Doctors table */ SELECT name, specialization FROM Doctors;