SQL syntax
SQL

SQL Syntax — Rules, Structure and How to Write SQL Statements

CHAPTER 2 of 30 SQL Tutorial — Free Course on Neotech Navigators
Before writing complex queries, you need to understand how SQL statements are structured. SQL syntax refers to the set of rules that define how SQL statements must be written so the database can understand and execute them. In this chapter, you will learn the building blocks of every SQL statement, formatting conventions, and common mistakes beginners make.

What Is SQL Syntax?

SQL syntax is the set of rules and structure that governs how you write instructions for a database. Think of it as the grammar of SQL — just like English has rules for forming sentences, SQL has rules for forming statements.

Every SQL statement is built from keywords, clauses, expressions, and operators arranged in a specific order. If you break a syntax rule, the database will return an error instead of a result.

Here is a simple example to see SQL syntax in action:

SQL
SELECT Name, City
FROM Customers
WHERE City = 'Mumbai'
ORDER BY Name;
✓ OUTPUT
+----------------+---------+
| Name           | City    |
+----------------+---------+
| Amit Patel     | Mumbai  |
| Rahul Sharma   | Mumbai  |
+----------------+---------+

This single statement contains four clausesSELECT, FROM, WHERE, and ORDER BY — each playing a specific role. Let’s break them down.

Structure of an SQL Statement

Most SQL statements follow a predictable structure. Here are the core components:

Component Purpose Example
Keywords Reserved words that tell the database what action to perform SELECT, FROM, WHERE
Clauses Sections of a statement, each beginning with a keyword WHERE City = 'Mumbai'
Expressions Values, column names, or calculations that produce a result Salary * 1.10
Operators Symbols that compare or combine values =, >, AND, OR
Semicolon Marks the end of a statement ;

💡 Key Rule

Every SQL query must have at least two clauses: SELECT (what columns to show) and FROM (which table to read from). Everything else — WHERE, ORDER BY, GROUP BY — is optional.

The SELECT Statement — Most Common SQL Syntax

The SELECT statement is the most frequently used SQL command. You will explore it in depth in Chapter 3: SQL SELECT. Here is its general syntax:

SQL — General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column ASC|DESC;

Let’s use the Employees table from Chapter 1 to see this in practice:

TABLE: Employees
+----+-----------+------------+--------+--------+
| ID | Name      | Department | Salary | City   |
+----+-----------+------------+--------+--------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai |
| 2  | John D.   | Marketing  | 62000  | London |
| 3  | Arun M.   | IT         | 78000  | Delhi  |
| 4  | Emma W.   | Sales      | 51000  | Sydney |
| 5  | Ravi S.   | IT         | 72000  | Mumbai |
+----+-----------+------------+--------+--------+

Example 1 — Select specific columns:

SQL
SELECT Name, Department
FROM Employees;
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| Priya K.  | Sales      |
| John D.   | Marketing  |
| Arun M.   | IT         |
| Emma W.   | Sales      |
| Ravi S.   | IT         |
+-----------+------------+

Example 2 — Select all columns using *:

SQL
SELECT *
FROM Employees;

The asterisk (*) is a shortcut that means “all columns.” It is useful for quick exploration but should be avoided in production queries because it fetches more data than you usually need.

SQL Keywords Are NOT Case Sensitive

One of the most important syntax rules to remember: SQL keywords are not case sensitive. All three statements below are identical and produce the exact same result:

SQL — All Three Are Valid
-- UPPERCASE (Recommended Convention)
SELECT Name FROM Employees;

-- lowercase (Also works)
select Name from Employees;

-- Mixed case (Also works)
SeLeCt Name FrOm Employees;

📌 Best Practice

Always write SQL keywords in UPPERCASE (SELECT, FROM, WHERE) and table/column names in their original case. This makes your queries much easier to read and is the industry-standard convention used by professional developers.

The Semicolon — Statement Terminator

The semicolon (;) marks the end of an SQL statement. It tells the database: “This is a complete instruction — execute it now.”

SQL
-- Single statement
SELECT Name FROM Employees;

-- Multiple statements executed together
SELECT Name FROM Employees;
SELECT City FROM Customers;

Some database systems (like MySQL) allow you to omit the semicolon for single queries. However, always include it — it is required when running multiple statements and is considered best practice in all environments.

Single Line vs Multi-Line Statements

SQL does not care about line breaks or extra spaces. A query can be written on a single line or spread across multiple lines. The database sees both versions identically.

Single line (harder to read):

SQL — Single Line
SELECT Name, Department, Salary FROM Employees WHERE Salary > 60000 ORDER BY Salary DESC;

Multi-line (much easier to read):

SQL — Multi-Line (Recommended)
SELECT Name, Department, Salary
FROM Employees
WHERE Salary > 60000
ORDER BY Salary DESC;
✓ OUTPUT (Same for Both)
+-----------+------------+--------+
| Name      | Department | Salary |
+-----------+------------+--------+
| Arun M.   | IT         | 78000  |
| Ravi S.   | IT         | 72000  |
| John D.   | Marketing  | 62000  |
+-----------+------------+--------+

💡 Formatting Tip

Always use the multi-line format — put each clause (SELECT, FROM, WHERE, ORDER BY) on its own line. This is the professional standard and makes debugging much easier, especially in long queries.

SQL Comments

Comments are notes you add to your SQL code for explanation. The database ignores them completely — they exist only for human readers. SQL supports two types of comments:

Single-Line Comments

Use two dashes (--) to comment a single line:

SQL
-- This query fetches employees earning over 60K
SELECT Name, Salary
FROM Employees
WHERE Salary > 60000;

Multi-Line Comments

Use /* ... */ to comment across multiple lines:

SQL
/*
  Author: PK
  Purpose: Get high-earning employees
  Date: March 2026
*/
SELECT Name, Salary
FROM Employees
WHERE Salary > 60000;

Adding comments to your queries is a highly recommended habit. When you revisit a query weeks later, comments help you instantly understand what the code does and why you wrote it.

Text Values vs Numeric Values

An important syntax rule in SQL is how you handle different data types in your conditions. You will use these rules heavily when learning the SQL WHERE clause in Chapter 5.

Text values must be enclosed in single quotes:

SQL — Text Values Use Quotes
SELECT * FROM Employees
WHERE City = 'Mumbai';       -- ✓ Correct

SELECT * FROM Employees
WHERE Department = 'IT';     -- ✓ Correct

Numeric values do NOT use quotes:

SQL — Numbers Without Quotes
SELECT * FROM Employees
WHERE Salary > 60000;          -- ✓ Correct

SELECT * FROM Employees
WHERE ID = 3;                  -- ✓ Correct

⚠️ Common Mistake

Wrapping numbers in quotes (WHERE Salary > '60000') can cause unexpected behavior in some databases. It may work, but the database treats the value as text and compares it alphabetically instead of numerically. Always keep numbers unquoted.

Most Important SQL Keywords

Here are the SQL keywords you will use most frequently throughout this SQL tutorial course. Each one will be covered in detail in upcoming chapters:

Keyword What It Does Chapter
SELECT Specifies which columns to retrieve Ch 3
FROM Specifies which table to query Ch 3
WHERE Filters rows based on a condition Ch 5
ORDER BY Sorts results in ascending or descending order Ch 7
INSERT INTO Adds new rows to a table Ch 8
UPDATE Modifies existing data in a table Ch 10
DELETE Removes rows from a table Ch 11
AND / OR Combines multiple conditions Ch 6
LIKE Searches for patterns in text Ch 15
JOIN Combines rows from two or more tables Ch 20
GROUP BY Groups rows that share a value Ch 25
COUNT / SUM / AVG Performs calculations on groups of rows Ch 14

For a complete reference of all SQL statements, see the official MySQL SQL statements documentation.

Common SQL Syntax Errors and How to Fix Them

As a beginner, you will encounter syntax errors frequently. Here are the most common mistakes and how to fix them:

Error Wrong Syntax Correct Syntax
Missing quotes around text WHERE City = Mumbai WHERE City = 'Mumbai'
Quotes around numbers WHERE ID = '3' WHERE ID = 3
Missing comma between columns SELECT Name City SELECT Name, City
Misspelled keyword SLECT Name FROM... SELECT Name FROM...
Missing FROM clause SELECT Name WHERE... SELECT Name FROM Table WHERE...
Wrong quote type WHERE City = "Mumbai" WHERE City = 'Mumbai'

💡 Debugging Tip

When you get a syntax error, read the error message carefully — it usually tells you the exact line and position where the problem is. Start by checking for missing commas, unmatched quotes, and misspelled keywords.

Complete SQL Statement Order

When writing a full SQL query, clauses must appear in a specific order. Here is the correct sequence that the database expects:

SQL — Correct Clause Order
SELECT    column1, column2      -- 1. What to show
FROM      table_name            -- 2. Where to look
WHERE     condition              -- 3. Which rows to filter
GROUP BY  column                 -- 4. How to group
HAVING    group_condition        -- 5. Filter groups
ORDER BY  column ASC|DESC       -- 6. How to sort
LIMIT     number;                -- 7. How many rows

You won’t always use all seven clauses. A simple query may only need SELECT and FROM. But when you use multiple clauses, they must appear in this order — putting WHERE after ORDER BY, for example, will cause a syntax error.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Using the Employees table, write a query that shows the Name and Salary of employees who earn more than 55000, sorted by salary from highest to lowest. Try writing it before looking at the answer below.

Your query should use: SELECT, FROM, WHERE, and ORDER BY

ANSWER
SELECT Name, Salary
FROM Employees
WHERE Salary > 55000
ORDER BY Salary DESC;

Expected Output:

+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Arun M.   | 78000  |
| Ravi S.   | 72000  |
| John D.   | 62000  |
+-----------+--------+

📝 What You Learned in This Chapter

  • SQL syntax is the set of rules defining how statements are structured
  • Every query needs at least SELECT and FROM
  • SQL keywords (SELECT, WHERE, etc.) are NOT case sensitive, but the convention is to write them in UPPERCASE
  • Semicolons (;) mark the end of a statement — always include them
  • Text values need single quotes ('Mumbai'); numbers do not
  • Use -- for single-line comments and /* */ for multi-line comments
  • SQL clauses must follow a specific order: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT

Frequently Asked Questions

Is SQL case sensitive?
SQL keywords like SELECT, FROM, and WHERE are not case sensitive — you can write them in uppercase, lowercase, or mixed case. However, data values inside tables may be case sensitive depending on the database system and its collation settings. For example, 'Mumbai' and 'mumbai' might be treated differently in PostgreSQL but the same in MySQL with default settings.
Do I always need a semicolon at the end of SQL statements?
It depends on the database system. MySQL and PostgreSQL can execute single queries without a semicolon, but it is always best practice to include one. When running multiple statements together, the semicolon is mandatory to separate them. Professional developers always use semicolons regardless of the environment.
Should I use single quotes or double quotes in SQL?
Use single quotes for text values ('Hello'). This is the SQL standard and works across all databases. Double quotes are used in some databases (like PostgreSQL) to reference column or table names that contain special characters or are case sensitive — but never for data values.
Can I write an entire SQL query on one line?
Yes, SQL ignores line breaks and extra white space. A query written on one line produces the exact same result as the same query spread across multiple lines. However, multi-line formatting with each clause on its own line is strongly recommended for readability, debugging, and professional coding standards.
What happens if I make a syntax error in SQL?
The database will not execute the query and will return an error message. The error message typically includes the line number and a description of what went wrong — such as a missing keyword, unmatched quote, or unexpected token. Reading error messages carefully is the fastest way to identify and fix syntax problems.

📖 Chapter 2 of 30 — SQL Tutorial on Neotech Navigators

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