SQL Syntax & Rules
Before you can write powerful SQL queries, you need to know the grammar. SQL has a small but precise set of rules — around semicolons, case, whitespace, identifiers, and comments — that every database engine enforces. Master these rules once and every SQL statement you ever write will be structured correctly.
Anatomy of an SQL Statement
Every SQL statement is built from clauses. A clause starts with a keyword (like SELECT or WHERE) and is followed by one or more expressions — column names, values, operators, or conditions. Together, the clauses form a complete instruction that the database engine can parse and execute.
The key insight: SQL is declarative. You describe what you want (all employees earning more than 50,000), not how to find it. The database engine figures out the most efficient route to that answer.
The 6 Core SQL Syntax Rules
These six rules govern every valid SQL statement. Violate any one of them and the database will throw a syntax error before executing a single byte.
; marks the end of a complete SQL statement. Most
database clients require it. Without one, they wait for more input or throw an error.
'Alice',
'New York'.
Double quotes are reserved for identifiers in standard SQL. Never use them for string
data.
Case Sensitivity — Keywords vs Data
SQL has a split personality when it comes to case: SQL keywords are always case-insensitive, but string data may or may not be, depending on the database and its collation settings.
| What | Case-sensitive? | Example | Notes |
|---|---|---|---|
| SQL keywords | ❌ No | select = SELECT = Select |
All identical to the parser |
| Table / column names | ⚠️ Depends | employees ≠ Employees (Linux MySQL) |
Linux file system is case-sensitive; Windows is not |
| String data values | ⚠️ Depends on collation | 'alice' = 'Alice' (MySQL utf8_general_ci) |
_ci = case-insensitive; _cs = case-sensitive |
| Quoted identifiers | ✅ Yes | "Name" ≠ "name" |
Backticks in MySQL, double quotes in PostgreSQL |
SELECT first_name, last_name FROM employees WHERE department_id = 10 ORDER BY last_name;
select First_Name, LAST_NAME From Employees where Department_ID = 10 order by last_name;
Semicolons & Whitespace
Semicolons
The semicolon ; is the statement terminator. It tells the
database engine "this statement is complete — go execute it."
-
Required in scripts with multiple statements Without semicolons, the parser cannot tell where one statement ends and the next begins.
-
Optional for single statements in some clients MySQL Workbench and many GUI tools add one automatically. The command-line client requires it.
-
Always add it — it never hurts Make the semicolon a habit from day one. It's always safe to include, never safe to omit in scripts.
CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(100));
INSERT INTO products (id, name)
VALUES (1, 'Laptop'), (2, 'Mouse');
SELECT * FROM products;
Whitespace
SQL is completely whitespace-agnostic between tokens. Spaces, tabs, and newlines are treated identically by the parser — use them to make your query readable, not for correctness.
SELECT
e.first_name,
e.last_name,
d.department_name
FROM
employees e
JOIN departments d ON e.dept_id = d.id
WHERE
e.salary > 50000
ORDER BY
e.last_name ASC;
SELECT e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.dept_id = d.id WHERE e.salary > 50000 ORDER BY e.last_name ASC;
'New York' ≠ 'NewYork' ≠ 'New York'.
Only whitespace outside quotes is ignored.
Identifiers & Naming Rules
An identifier is any name you give to a database object — a table, column, index, view, alias, or schema. Identifiers must follow these rules to be valid:
-
Start with a letter or underscore Valid:
employee_id,_temp. Invalid:1employee,-name. -
Only letters, digits, and underscores after that No hyphens, spaces, or special characters (unless the identifier is quoted).
-
Must not be a reserved word (unquoted) Words like SELECT, FROM, WHERE, ORDER are reserved — using them as column names without quotes causes errors.
-
No length limit in practice, but keep them short MySQL allows up to 64 characters. SQL Server allows up to 128. Be descriptive but concise.
Naming Convention Comparison
| Convention | Example | Common in | Recommended? |
|---|---|---|---|
| snake_case | employee_id, first_name | PostgreSQL, MySQL, standard SQL | ✅ Yes — most universal |
| PascalCase | EmployeeId, FirstName | SQL Server, .NET environments | ✅ Acceptable in SQL Server |
| camelCase | employeeId, firstName | Rarely, usually from ORM output | ⚠️ Avoid — requires quoting |
| Spaces / hyphens | "first name", "emp-id" | Accidental / legacy | ❌ No — always requires quoting |
| ALL_CAPS | EMPLOYEE_ID, FIRST_NAME | Oracle legacy | ⚠️ Outdated — hard to distinguish from keywords |
Quoting Identifiers
If you must use reserved words or special characters as identifiers, wrap them in the database-specific quote character:
SELECT `name`, `order` FROM `user-data`;
SELECT "name", "order" FROM "user-data";
snake_case names that are descriptive, avoid reserved words, and your queries will
be portable across every SQL database.
SQL Comments
Comments let you annotate your SQL without affecting execution. The database parser skips everything inside a comment. SQL supports two comment styles:
-- comments extend to the end of the line. Everything after the
double dash on that line is ignored.
/* … */ comments span multiple lines and can be placed anywhere —
even in the middle of a statement.
Use -- for quick notes and /* */ for headers and multi-line
explanations.
Required Clause Order in SELECT
The clauses of a SELECT statement must appear in a fixed sequence. The parser will throw an error if you put WHERE after GROUP BY, for example. Memorise this order — it becomes second nature.
Common SQL Syntax Mistakes
These are the errors beginners hit most often. Recognise them now and save yourself hours of head-scratching later.
1. Using double quotes for string values
SELECT * FROM users WHERE name = "Alice";
SELECT * FROM users WHERE name = 'Alice';
In MySQL, double quotes can work in some modes, but in standard SQL (PostgreSQL, Oracle) double quotes are reserved for identifiers. Always use single quotes for string data.
2. Missing the FROM clause
SELECT name, salary WHERE salary > 50000;
SELECT name, salary FROM employees WHERE salary > 50000;
3. Using WHERE with aggregate functions
SELECT dept_id, AVG(salary) FROM employees WHERE AVG(salary) > 60000 GROUP BY dept_id;
SELECT dept_id, AVG(salary) FROM employees GROUP BY dept_id HAVING AVG(salary) > 60000;
4. Wrong clause order
SELECT name FROM employees ORDER BY name WHERE salary > 40000;
SELECT name FROM employees WHERE salary > 40000 ORDER BY name;
5. Using = for NULL comparison
SELECT * FROM employees WHERE manager_id = NULL;
SELECT * FROM employees WHERE manager_id IS NULL;
NULL means "unknown value." In SQL,
NULL = NULL evaluates to NULL (not TRUE), so the equals operator never
matches NULL. Always use IS NULL or IS NOT
NULL.
Try It Yourself — SQL Syntax Playground
Practice the syntax rules you just learned using the in-browser runner below. The sample
database includes an employees table. Try experimenting with different queries.
employees · Press Ctrl + Enter to
run
Which of the following is the correct way to filter rows where a column is NULL in SQL?
- An SQL statement is made of clauses — each starting with a keyword like SELECT, FROM, or WHERE — followed by expressions.
- SQL is declarative: you describe what data you want, not how to find it.
- SQL keywords are case-insensitive; string data case sensitivity depends on the database collation. Convention: UPPERCASE keywords, lowercase identifiers.
- Always use single quotes for string values (
'Alice') and reserve double quotes / backticks for identifiers. - The semicolon
;terminates a statement — always include it, especially in scripts with multiple statements. - Whitespace outside string literals is completely ignored by the parser — use it freely for readability.
- Identifiers must start with a letter or underscore, contain only alphanumeric characters and underscores, and must not clash with reserved words.
- Use
--for single-line comments and/* … */for multi-line comments. - Clauses in a SELECT must follow the fixed order: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.
- The most common mistakes: double quotes for strings, = for NULL comparisons, WHERE with aggregates instead of HAVING, and wrong clause order.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.
--/* … */