SQL Tutorial › Getting Started › SQL Syntax & Comments

SQL Syntax and Comments

BeginnerLesson 3 of 699 min read9 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • 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

SQL
SELECT first_name, salaryFROM employeesWHERE dept_id = 2;
ResultMySQL 8.4
first_namesalary
Vikram105000
Neha54000
Arjun48000
3 rows in set
PartExampleMeaning
KeywordsSELECT, FROM, WHEREReserved words with a fixed meaning
Identifiersfirst_name, employeesNames of columns, tables, databases
Literals2, 'Mumbai'Fixed values: numbers, text, dates
Operators=, >, ANDCompare or combine values
ClausesWHERE dept_id = 2A keyword plus what follows it
Terminator;Ends the statement
How this query works, step by step
  1. FROM employees names the table.
  2. WHERE dept_id = 2 keeps the Sales department rows (dept 2 is Sales).
  3. SELECT first_name, salary shows 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:

SQL
select first_name from employees where emp_id = 101;
ResultMySQL 8.4
first_name
Asha
1 row in set

Text comparisons are also case insensitive by default in MySQL, because the default collation (utf8mb4_0900_ai_ci) ends in ci, meaning case-insensitive:

MySQL
SELECT first_name FROM employees WHERE city = 'PUNE';
ResultMySQL 8.4
first_name
Priya
Kavya
Rohan
3 rows in set
Note

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:

SQL
SELECT   first_name,         job_titleFROM     employeesWHERE    salary >= 100000;
ResultMySQL 8.4
first_namejob_title
MeeraCEO
AshaEngineering Manager
VikramSales Manager
3 rows in set

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:

SQL
SELECT COUNT(*) AS departments FROM departments;SELECT COUNT(*) AS customers FROM customers;
ResultMySQL 8.4
mysql> SELECT COUNT(*) AS departments FROM departments
departments
5
1 row in set
mysql> SELECT COUNT(*) AS customers FROM customers
customers
7
1 row in set

Rule 4: quotes

QuoteUse forExample
Single quotesText and date valuesWHERE city = 'Pune'
Backticks (the key left of 1)Names with spaces or reserved wordssee the example below
Double quotesText in MySQL (names in standard SQL)avoid; use single quotes

To put a single quote inside text, double it:

SQL
SELECT 'It''s a test' AS message, 'O''Brien' AS surname;
ResultMySQL 8.4
messagesurname
It's a testO'Brien
1 row in set
How this query works, step by step
  1. Inside single-quoted text, '' (two single quotes) stands for one '.
  2. 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:

SQL
SELECT dept_name AS `Department Name`, location AS `where`FROM departmentsWHERE dept_id <= 2;
ResultMySQL 8.4
Department Namewhere
EngineeringBengaluru
SalesMumbai
2 rows in set

Rule 5: numbers, text and NULL

MySQL
SELECT 42 AS whole_number, 3.75 AS decimal_number, 'hello' AS text_value, '2026-01-31' AS date_text, NULL AS nothing;
ResultMySQL 8.4
whole_numberdecimal_numbertext_valuedate_textnothing
423.75hello2026-01-31NULL
1 row in set
  • Numbers are written without quotes.
  • Text and dates use single quotes. MySQL dates are written 'YYYY-MM-DD'.
  • NULL means “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.

SQL
-- 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);
ResultMySQL 8.4
first_namesalary
Asha125000
Rahul98000
2 rows in set
How this query works, step by step
  1. -- followed by a space starts a comment that runs to the end of the line. MySQL requires the space after the two dashes.
  2. /* ... */ can sit inside a line or span many lines.
  3. MySQL also accepts # as a single-line comment, but -- works in every database, so prefer it.
Tip

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, not s1.
  • Pick singular or plural table names and stay consistent. This tutorial uses plural (employees).
  • Avoid reserved words like order, group or select as names; you would have to quote them everywhere.

Quick quiz

1. Which quotes should you use for a text value in MySQL?

2. Is select * from employees; valid?

3. How do you write the text It’s in SQL?

4. What must follow -- to start a comment in MySQL?

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.