SQL syntax
SQL

SQL Syntax: Rules, Statement Structure and Clause Order

SQL Tutorial · Chapter 2 of 32

SQL syntax is the set of rules that decides how keywords, table names, column names and values are arranged into a valid statement. Every SQL statement is made of clauses that must appear in a fixed order, text values go in single quotes, and statements end with a semicolon. Learn these rules once and every later chapter becomes easier.

Anatomy of a SQL Statement

SELECT   Name, City          -- which columns
FROM     Customers           -- which table
WHERE    Country = 'India'   -- which rows
ORDER BY Name;               -- in which order

Each line starting with a keyword is a clause. SELECT and FROM are required for a query; the rest are optional but, when used, must follow the order shown in the next section.

Sample Table

Customers
CustomerID Name City Country
1 Anita Sharma Mumbai India
2 John Smith London UK
3 Maria Garcia Madrid Spain
4 Wei Chen Singapore Singapore
5 Emma Brown Sydney Australia

Clause Order Rule

A SELECT statement always follows this sequence. You may skip clauses, but you may never swap them.

Order Clause Job
1 SELECT Columns to return
2 FROM Table(s) to read
3 WHERE Filter individual rows
4 GROUP BY Group rows for aggregation
5 HAVING Filter groups
6 ORDER BY Sort the result
7 LIMIT / TOP / FETCH Restrict row count

Example 1: Keywords Are Not Case Sensitive

These two statements are identical to the database. Upper-case keywords are a readability convention, not a requirement.

select name, city from customers where country = 'UK';
SELECT Name, City FROM Customers WHERE Country = 'UK';
Name City
John Smith London

Note that the value 'UK' can be case sensitive depending on the database, which is covered below.

Example 2: Text Needs Quotes, Numbers Do Not

SELECT Name FROM Customers WHERE CustomerID = 3;
SELECT Name FROM Customers WHERE City = 'Madrid';
Name
Maria Garcia

Both queries return the same row. Writing WHERE City = Madrid without quotes makes the database look for a column called Madrid and raises an error.

Example 3: Whitespace and Line Breaks Are Free

SQL ignores extra spaces and new lines, so a statement can sit on one line or be spread over many. Multi-line formatting is the professional habit.

SELECT
    Name,
    Country
FROM Customers
WHERE Country = 'Australia';
Name Country
Emma Brown Australia

Example 4: Comments

-- Single-line comment
SELECT Name FROM Customers /* inline comment */ WHERE CustomerID = 1;
Name
Anita Sharma

Semicolons and Multiple Statements

The semicolon marks the end of a statement. Many tools accept a single statement without it, but it is required when several statements run together in a script, so always include it.

Works in MySQL, SQL Server, PostgreSQL, SQLite

  • Quoting identifiers: names with spaces need "double quotes" in PostgreSQL and SQLite, `backticks` in MySQL and [square brackets] in SQL Server.
  • Text comparison: MySQL and SQL Server compare text case-insensitively by default; PostgreSQL and SQLite are case sensitive, so 'uk' and 'UK' differ.
  • Double quotes for strings: MySQL and SQLite tolerate "text", but stick to single quotes for portable SQL.

Common Mistakes

  • Putting WHERE after ORDER BY or GROUP BY before WHERE; clause order is fixed.
  • Trailing comma in the column list: SELECT Name, City, FROM is a syntax error.
  • Using double quotes for text values and then wondering why the query fails in PostgreSQL.
  • Ending a comment line with the semicolon inside it, so the statement never terminates.

Practice Exercise

  1. Rewrite this statement in the correct clause order: SELECT Name FROM Customers ORDER BY Name WHERE Country = 'Spain';
  2. Fix the error: SELECT Name, FROM Customers WHERE City = London;
Show answers
-- 1
SELECT Name FROM Customers WHERE Country = 'Spain' ORDER BY Name;

-- 2 (remove the trailing comma, quote the text value)
SELECT Name FROM Customers WHERE City = 'London';

Related Chapters

FAQ

Is SQL case sensitive?

Keywords and, in most systems, table and column names are not case sensitive. Text values inside quotes may be, depending on the database: PostgreSQL and SQLite treat ‘UK’ and ‘uk’ as different values.

Do I have to end every SQL statement with a semicolon?

Single statements usually run without one, but scripts containing several statements require semicolons to separate them. Including it every time is the safest habit.

Can I write a SQL query on one line?

Yes. SQL ignores line breaks and extra spaces. Splitting clauses across lines simply makes long queries easier to read and debug.

Chapter 2 of 32 · SQL from Scratch: all 32 chapters

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com