SQL Syntax and Comments
SQL has a small set of syntax rules, and once you know them you can read almost any query. This lesson covers how a statement is built, when case matters, how to quote text and names, and how to write comments, with every rule shown on a real MySQL 8.4 query.
- The parts of a SQL statement: keywords, identifiers, literals and clauses
- When SQL is and is not case sensitive
- Single quotes for text, backticks for names
- Single-line and multi-line comments
Anatomy of a SQL statement
SELECT first_name, salaryFROM employeesWHERE dept_id = 2;| first_name | salary |
|---|---|
| Vikram | 105000 |
| Neha | 54000 |
| Arjun | 48000 |
| Part | Example | Meaning |
|---|---|---|
| Keywords | SELECT, FROM, WHERE | Reserved words with a fixed meaning |
| Identifiers | first_name, employees | Names of columns, tables, databases |
| Literals | 2, 'Mumbai' | Fixed values: numbers, text, dates |
| Operators | =, >, AND | Compare or combine values |
| Clauses | WHERE dept_id = 2 | A keyword plus what follows it |
| Terminator | ; | Ends the statement |
FROM employeesnames the table.WHERE dept_id = 2keeps the Sales department rows (dept 2 is Sales).SELECT first_name, salaryshows two columns: three rows come back.
Rule 1: keywords are not case sensitive
SELECT, select and SeLeCt all work. Writing keywords in UPPER CASE and names in lower case is the usual style because it makes queries easier to scan:
select first_name from employees where emp_id = 101;| first_name |
|---|
| Asha |
Text comparisons are also case insensitive by default in MySQL, because the default collation (utf8mb4_0900_ai_ci) ends in ci, meaning case-insensitive:
SELECT first_name FROM employees WHERE city = 'PUNE';| first_name |
|---|
| Priya |
| Kavya |
| Rohan |
Other databases differ. In PostgreSQL 'PUNE' = 'Pune' is false. Table names are case sensitive in MySQL on Linux but not on Windows, so always write names exactly as created.
Rule 2: whitespace and line breaks do not matter
You may put a whole query on one line or spread it over many. Spread long queries one clause per line, so they are easy to read and to compare in code reviews:
SELECT first_name, job_titleFROM employeesWHERE salary >= 100000;| first_name | job_title |
|---|---|
| Meera | CEO |
| Asha | Engineering Manager |
| Vikram | Sales Manager |
Rule 3: end statements with a semicolon
The semicolon separates statements. In the mysql client a statement does not run until you type ;. When you send several statements at once, each needs one:
SELECT COUNT(*) AS departments FROM departments;SELECT COUNT(*) AS customers FROM customers;| departments |
|---|
| 5 |
| customers |
|---|
| 7 |
Rule 4: quotes
| Quote | Use for | Example |
|---|---|---|
| Single quotes | Text and date values | WHERE city = 'Pune' |
| Backticks (the key left of 1) | Names with spaces or reserved words | see the example below |
| Double quotes | Text in MySQL (names in standard SQL) | avoid; use single quotes |
To put a single quote inside text, double it:
SELECT 'It''s a test' AS message, 'O''Brien' AS surname;| message | surname |
|---|---|
| It's a test | O'Brien |
- Inside single-quoted text,
''(two single quotes) stands for one'. - MySQL returns the text with one quote, as the result shows.
Names normally need no quotes. Backticks are only needed when a name is a reserved word or contains spaces or special characters:
SELECT dept_name AS `Department Name`, location AS `where`FROM departmentsWHERE dept_id <= 2;| Department Name | where |
|---|---|
| Engineering | Bengaluru |
| Sales | Mumbai |
Rule 5: numbers, text and NULL
SELECT 42 AS whole_number, 3.75 AS decimal_number, 'hello' AS text_value, '2026-01-31' AS date_text, NULL AS nothing;| whole_number | decimal_number | text_value | date_text | nothing |
|---|---|---|---|---|
| 42 | 3.75 | hello | 2026-01-31 | NULL |
- Numbers are written without quotes.
- Text and dates use single quotes. MySQL dates are written
'YYYY-MM-DD'. NULLmeans “no value” and has its own lesson: NULL Values.
Comments
Comments are ignored by the database. Use them to explain why a query does something.
-- single-line comment: everything after "-- " is ignoredSELECT first_name, -- the employee's first name salary /* monthly salary in rupees */FROM employees/* a multi-line comment can span several lines */WHERE emp_id IN (101, 102);| first_name | salary |
|---|---|
| Asha | 125000 |
| Rahul | 98000 |
--followed by a space starts a comment that runs to the end of the line. MySQL requires the space after the two dashes./* ... */can sit inside a line or span many lines.- MySQL also accepts
#as a single-line comment, but--works in every database, so prefer it.
Commenting out a line is a quick way to test a query without one condition: put -- in front of the WHERE line and run it again.
Naming conventions
- Use lower case with underscores:
order_items,hire_date. - Make names describe the data:
salary, nots1. - Pick singular or plural table names and stay consistent. This tutorial uses plural (
employees). - Avoid reserved words like
order,grouporselectas names; you would have to quote them everywhere.
Quick quiz
1. Which quotes should you use for a text value in MySQL?
Single quotes mark text and date literals in every SQL database.
2. Is select * from employees; valid?
Upper case keywords are a convention, not a rule.
3. How do you write the text It’s in SQL?
Doubling the quote escapes it.
4. What must follow -- to start a comment in MySQL?
MySQL treats -- as a comment only when it is followed by whitespace.
SQL syntax FAQ
Is SQL case sensitive?
SQL keywords are not case sensitive. In MySQL, text comparisons are case insensitive with the default collation, and table names are case sensitive on Linux but not on Windows.
Is the semicolon required in SQL?
It is required to separate statements and to run a statement in the mysql command-line client. A single statement sent by a tool or program usually works without it, but adding it is good practice.
How do I write comments in SQL?
Use -- for a single-line comment and /* ... */ for a comment that spans several lines. MySQL also supports #.
What is the difference between single and double quotes in SQL?
Standard SQL uses single quotes for text and double quotes for identifiers. MySQL accepts double quotes for text by default, but single quotes work everywhere, so use them for text and backticks for names that need quoting.