Skip to content
Laviyx Learn

Try

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.

Beginner7 min readUpdated
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:

SQLRun
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 order

Clauses 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:

SQL
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:

SQLRun
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:

SQLRun
-- 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:

SQLRun
SELECT name, salary FROM employees WHERE department = 'Sales' ORDER BY salary DESC;
SQLRun
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:

QuoteUsed forExample
'single'Text values (strings)WHERE city = 'Mumbai'
"double"Names of columns or tables that need quotingAS "Yearly Salary"
noneNumbers, keywords, ordinary namesWHERE salary > 50000

To put a single quote inside text, write it twice:

SQLRun
SELECT 'Priya''s team' AS label;
Output
label
------------
Priya's team

Comments#

Comments are notes for humans; the database ignores them.

SQLRun
-- 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:

  1. Keywords in UPPERCASE, names in lowercase snake_case (hire_date).
  2. One major clause per line.
  3. Indent continuation lines, such as extra AND conditions.
  4. Name things clearly: orders, not tbl1.
  5. List columns explicitly instead of SELECT * in finished queries.
SQLRun
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:

SQL
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
  1. Remove the trailing comma after city.
  2. Move WHERE before ORDER BY.
  3. Use single quotes for the text value.
SQLRun
SELECT name, city
FROM employees
WHERE city = 'Mumbai'
ORDER BY name;
Output
name | city
-----+-------
Asha | Mumbai
John | Mumbai

Writing the keywords in uppercase was not required, but it makes the corrected query easier to read.

Key takeaway#

Finished this lesson?