SQL course menu
SQL Syntax Rules: How to Write Readable SQL Statements
Learn the basic rules of SQL syntax: statements, clauses, keywords, semicolons, quotes, case sensitivity, whitespace, and comments, plus a simple style guide for readable queries.
On this page
Every language has grammar. SQL's grammar is small and forgiving, but a few rules cause most beginner errors. This lesson covers them, along with a simple style that keeps your queries easy to read.
Statements and clauses#
A statement is one complete instruction to the database. A statement is made of clauses, each starting with a keyword:
SELECT name, salary -- SELECT clause: which columns
FROM employees -- FROM clause: which table
WHERE salary > 60000 -- WHERE clause: which rows
ORDER BY salary DESC; -- ORDER BY clause: in what orderClauses must appear in a fixed order. You can leave optional ones out, but you cannot reorder them. WHERE before FROM is a syntax error.
The usual order of clauses in a SELECT statement is:
SELECT ...
FROM ...
JOIN ... ON ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...;Semicolons end statements#
A semicolon ; marks the end of a statement. It is required when you run several statements together:
SELECT name FROM customers;
SELECT order_id FROM orders;Many tools let you run a single statement without one, but adding it every time is a good habit.
Keywords are not case-sensitive#
SELECT, select and Select all work. The common convention is UPPERCASE keywords and lowercase names, so the structure of a query stands out:
-- All of these are the same query
select name from employees;
SELECT name FROM employees;
SeLeCt name FrOm employees;Whitespace and line breaks do not matter#
SQL ignores extra spaces, tabs and new lines. These two queries are identical to the database:
SELECT name, salary FROM employees WHERE department = 'Sales' ORDER BY salary DESC;SELECT name, salary
FROM employees
WHERE department = 'Sales'
ORDER BY salary DESC;The second is much easier for people to read. Put each major clause on its own line.
Quotes: single for text, double for names#
This is the rule that trips up beginners most often:
To put a single quote inside text, write it twice:
SELECT 'Priya''s team' AS label;label
------------
Priya's teamComments#
Comments are notes for humans; the database ignores them.
-- A single-line comment starts with two dashes
SELECT name, salary
FROM employees
/* A block comment
can span several lines */
WHERE salary > 60000;Use comments to explain why a query does something unusual, not to repeat what the SQL already says.
Reserved words as names#
Words like order, group, select and user are reserved keywords. Naming a table order forces you to quote it every time ("order" or `order`). Prefer plural or more specific names such as orders or user_accounts.
A simple style guide#
These habits make SQL easier to read and review:
- Keywords in UPPERCASE, names in lowercase snake_case (
hire_date). - One major clause per line.
- Indent continuation lines, such as extra
ANDconditions. - Name things clearly:
orders, nottbl1. - List columns explicitly instead of
SELECT *in finished queries.
SELECT e.name,
e.department,
e.salary
FROM employees AS e
WHERE e.department = 'Engineering'
AND e.salary > 75000
ORDER BY e.salary DESC;Common mistakes#
Practice#
Practice exercise
This query has three mistakes. Find and fix them:
select name, city,
from employees
order by name
where city = "Mumbai"Show hint
Look at the comma after city, the order of the clauses, and the kind of quotes around Mumbai.
Show solution
- Remove the trailing comma after
city. - Move
WHEREbeforeORDER BY. - Use single quotes for the text value.
SELECT name, city
FROM employees
WHERE city = 'Mumbai'
ORDER BY name;name | city
-----+-------
Asha | Mumbai
John | MumbaiWriting the keywords in uppercase was not required, but it makes the corrected query easier to read.
Key takeaway#
Finished this lesson?