Skip to content

MySQL Tutorial SQL Basics

SQL Syntax Rules

Lesson 15 of 308 2 min read By Deepak Paswan Updated Oct 5, 2026

SQL looks like English, but it has strict rules. If you know them, most beginner errors disappear.

Rule 1: clauses come in a fixed order

For a SELECT, the clauses must appear in this order:

Text
PLAIN
SELECT   what to show
FROM     which table
WHERE    which rows
GROUP BY how to group
HAVING   which groups
ORDER BY how to sort
LIMIT    how many

You can leave clauses out, but you cannot reorder them.

query.sql
SQL
SELECT name FROM (SELECT 'Asha' AS name) AS t LIMIT 1 WHERE name = 'Asha';
Output
OUTPUT
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE name = 'Asha''

The error points near WHERE, because WHERE cannot come after LIMIT. MySQL shows the text from where it got lost.

Rule 2: commas separate items, never end a list

query.sql
SQL
SELECT 1 AS a, 2 AS b, 3 AS c;
Output
OUTPUT
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
1 row in set

A comma after the last item (3 AS c,) is a syntax error. This is the most common typing mistake when you edit a list of columns.

Rule 3: parentheses group things

Parentheses are used for lists of columns and values, sub-queries, function arguments and to control calculation order:

query.sql
SQL
SELECT (2 + 3) * 4 AS with_brackets, 2 + 3 * 4 AS without_brackets;
Output
OUTPUT
+---------------+------------------+
| with_brackets | without_brackets |
+---------------+------------------+
|            20 |               14 |
+---------------+------------------+
1 row in set

Every opening bracket needs a closing one. Many editors highlight the matching pair.

Rule 4: text goes in quotes, names do not

  • Text values use single quotes: 'Pune'.
  • Table and column names are written as they are, or in backticks if they need protection (see Identifiers and Backticks).
query.sql
SQL
SELECT 'name' AS text_value, 5 AS number_value;
Output
OUTPUT
+------------+--------------+
| text_value | number_value |
+------------+--------------+
| name       |            5 |
+------------+--------------+
1 row in set

Rule 5: whitespace is free

Spaces, tabs and new lines are ignored between words. A single statement can run over twenty lines. You may not split a word in half or put a space inside a name.

Rule 6: the order of execution is not the order of writing

You write SELECT first, but MySQL logically starts with FROM, then WHERE, GROUP BY, HAVING, then SELECT, then ORDER BY and LIMIT. That is why you cannot use a column alias from SELECT inside WHERE:

query.sql
SQL
CREATE TABLE items (id INT, price INT);
INSERT INTO items VALUES (1, 50), (2, 150);
SELECT id, price * 2 AS double_price FROM items WHERE double_price > 100;
Output
OUTPUT
ERROR 1054 (42S22): Unknown column 'double_price' in 'where clause'

The alias double_price does not exist yet when WHERE runs. Repeat the expression in WHERE, or use a sub-query:

query.sql
SQL
SELECT id, price * 2 AS double_price FROM items WHERE price * 2 > 100;
Output
OUTPUT
+------+--------------+
| id   | double_price |
+------+--------------+
|    2 |          300 |
+------+--------------+
1 row in set
info
ORDER BY is different
ORDER BY runs after SELECT, so it can use a column alias. MySQL also lets GROUP BY and HAVING use aliases. Standard SQL does not allow that, so it is better not to rely on it.
lightbulb
Read errors left to right
The message says “syntax error near ‘…'”. Look at the start of that quoted part. The real mistake is usually just before it.