Pathubs SQL Curriculum • Module 00

Basic SQL Syntax & Query Anatomy

Master the grammar of databases: understand statements, keywords, identifiers, clauses, expressions, statement categories, and clean SQL formatting.

⏱️ Estimated Time:35 Minutes
🎯 Level:Beginner Foundation
📊 Track:Data Analytics & SQL Mastery
✨ Mode:Interactive Syntax Explorer
1

Introduction

SQL (Structured Query Language) is the universal language used to communicate with relational databases. Just like natural human languages have rules of grammar and punctuation, SQL has a defined syntax — a set of rules governing how words, symbols, and clauses must be assembled to form valid commands.

Before diving into complex multi-table joins or analytical window functions, learning how to read and parse SQL syntax gives you the mental framework to understand any query at first glance.

2

What Is a SQL Statement?

A SQL statement is a complete, executable instruction sent to the database. Every statement is constructed from smaller fundamental building blocks:

Building BlockDefinitionExample in SQL
StatementThe entire complete instruction sent to the databaseSELECT name FROM employees;
KeywordReserved command words predefined by the SQL languageSELECT, FROM, WHERE, AS
IdentifierNames of user-created database objects (tables, columns, views)employees, salary, department
ClauseA major sub-section of a statement initiated by a keywordFROM employees, WHERE salary > 50000
OperatorSymbols or keywords that perform comparisons or arithmetic=, >, <, +, *
Value (Literal)Explicit constant data values (text strings, numbers, dates)'Sales', 75000, '2026-01-15'
ExpressionA combination of columns, operators, and values that evaluates to a single resultsalary * 1.10, price * quantity
Diagram 1: Anatomy of a SQL Statement
SELECT name, salary← [Keyword] + [Column Identifiers]
FROM employees← [Keyword] + [Table Identifier] (FROM Clause)
WHERE department = 'Sales';← [Clause] with [Identifier] [Operator] [Value]
3

SQL's Basic Structure

Unlike procedural programming languages (like Python or C++) where you specify step-by-step algorithms, SQL is a declarative language. You declare what data you need, and the database query planner optimizes how to retrieve it.

-- Structural Pipeline of a SQL Statement
SQL StatementParsed Clauses & TokensExecution PlannerResult Set / Table Change

Different SQL statements serve different purposes (e.g., retrieving records vs creating tables), but they all follow this predictable token structure.

4

SQL Keywords and Identifiers

One of the most essential distinctions in database programming is the difference between Keywords and Identifiers:

🔑 Keywords (Reserved Words)

Words reserved by SQL that define grammar actions. You cannot use them as table or column names without special escaping.

SELECT, FROM, WHERE, INSERT, UPDATE, DELETE, CREATE
🏷️ Identifiers (Database Objects)

Names created by developers and database administrators to identify tables, columns, views, and schemas.

employees, customers, order_total, first_name, city
5

SQL Statements and Semicolons (;)

The semicolon (;) is the standard statement terminator in SQL. It tells the query processor that a complete instruction has ended.

SELECT name FROM employees;
SELECT title FROM projects;
ℹ️
Tool & Client Note: In single-query GUI windows or web sandboxes, the query tool may execute a single statement even if the trailing semicolon is omitted. However, when executing multi-statement scripts, batch migrations, or terminal CLI commands (e.g. psql, mysql), semicolons are mandatory to distinguish individual commands.
6

SQL Comments

Comments allow developers to document query logic, explain business requirements, or temporarily disable clauses during debugging without affecting query execution:

-- 1. Single-Line Comment: Everything after two hyphens is ignored by SQL
SELECT name, salary
FROM employees;

/*
   2. Multi-Line Comment Block
   Author: Data Analytics Team
   Purpose: Extract active Q3 marketing leads
*/

SELECT email, signup_date
FROM leads;
7

SQL Naming Basics

Clear naming conventions make schemas intuitive and maintainable:

  • Use Lowercase with Snake_Case: Name tables and columns using lowercase letters separated by underscores (e.g. order_items, created_at, unit_price).
  • Use Plural for Tables: Standard convention recommends plural nouns for tables representing collections of entities (e.g. customers, products, invoices).
  • Use Singular for Primary Keys: Primary key columns are typically named id or customer_id.
  • Descriptive Aliases: When calculating virtual columns, use clear aliases (e.g. AS total_discounted_price instead of AS x).
8

Understanding Clauses

A clause is an independent grammatical block within a SQL statement. The most common querying clauses include:

SELECT name, salary        -- SELECT Clause: Declares output columns
FROM employees             -- FROM Clause: Identifies source table
WHERE department = 'Sales'; -- WHERE Clause: Specifies row filter

Each clause performs a dedicated responsibility in the query pipeline.

9

Common SQL Statement Categories

SQL commands are categorized into four major sub-languages based on their functional role:

CategoryFull NamePrimary CommandsPurpose
DQLData Query LanguageSELECTRetrieve and read data records from tables
DMLData Manipulation LanguageINSERT, UPDATE, DELETEAdd, modify, or remove data rows inside existing tables
DDLData Definition LanguageCREATE, ALTER, DROPDefine, modify, or delete table structures and database schemas
TCLTransaction Control LanguageCOMMIT, ROLLBACKManage transactional integrity and save or undo changes
Diagram 2: SQL Statement Categories Hierarchy
DQL (Querying)
SELECT
DML (Manipulation)
INSERT • UPDATE • DELETE
DDL (Definition)
CREATE • ALTER • DROP
TCL (Transactions)
COMMIT • ROLLBACK
10

Reading SQL From Left to Right

When encountering an unfamiliar SQL query, follow this 4-step mental reading workflow:

  1. Spot the Keywords: Scan for SELECT, FROM, WHERE to map the overall sentence skeleton.
  2. Find the Source Table: Look at the FROM clause to identify where the data lives.
  3. Identify the Projections: Check what columns or expressions are requested in the SELECT clause.
  4. Review the Conditions: Read any filter conditions in the WHERE clause to understand which subset of rows is kept.
Diagram 3: SQL Reading Mental Model
1. Scan Keywords
(SELECT / FROM / WHERE)
2. Locate Table
(FROM employees)
3. Check Columns
(name, salary)
4. Understand Goal
(Filtered Result Set)
11

SQL Formatting and Readability

SQL engines ignore extra whitespace and line breaks. However, readable formatting is essential for collaboration, code reviews, and debugging:

❌ Messy Single-Line Query:
select name,salary,email from employees where salary>50000 and status='active';
✅ Clean Formatted Query:
SELECT
  name,
  salary,
  email
FROM employees
WHERE salary > 50000;
12

Common Beginner Syntax Mistakes

1. Missing Commas in SELECT list

Writing SELECT name department salary FROM employees; triggers a syntax error. Columns must be comma-delimited.

2. Missing Quotes around String Literals

Writing WHERE city = Mumbai; causes SQL to look for a column named Mumbai. String values require quotes: 'Mumbai'.

3. Incorrect Clause Sequence

Writing SELECT name WHERE id = 5 FROM employees; is invalid. Clause order must be SELECT → FROM → WHERE.

4. Misspelled Keywords

Typos like SELCT or FORM will immediately fail query parsing.

Live Interactive SQL Syntax Explorer
Click any query token below to inspect its syntax role
SQL Keyword & Clause HeaderSELECT
What It Is
A reserved SQL keyword that initiates data extraction.
What Role It Plays
Tells the database query engine that you want to retrieve and read data without altering it.
Why It Is There
Every data extraction query in SQL must begin with SELECT.
🧩 Build the Query: Assemble the Clause Sequence

Click chips from the pool below to construct a syntactically valid SQL statement in order:

Click chips below in order: [SELECT] ➔ [Columns] ➔ [FROM] ➔ [Table] ➔ [WHERE] ➔ [Condition]
🛠️ Interactive Challenge: Fix The Syntax (1 of 4)

Fix the syntax error in the SELECT projection list.

SELECT first_name last_name, email FROM customers;
13

Practical SQL Reading Exercises

SQL StatementIdentified ClausesKey IdentifiersStatement Intent
SELECT title, price FROM courses;SELECT, FROMtitle, price, coursesExtract course titles and prices
SELECT DISTINCT city FROM customers;SELECT DISTINCT, FROMcity, customersExtract unique list of customer cities
SELECT name, salary * 12 AS annual FROM employees;SELECT, FROMname, salary, annualCalculate annual salary expression
INSERT INTO logs (message) VALUES ('OK');INSERT INTO, VALUESlogs, messageDML command inserting a new row
CREATE TABLE tags (id INT, label VARCHAR(50));CREATE TABLEtags, id, labelDDL command defining new table schema
14

Basic SQL Syntax Best Practices

  • Capitalize All Keywords: Write SELECT, FROM, WHERE in uppercase to visually isolate commands from table/column names.
  • Use Snake_Case for Identifiers: Keep table and column names in lowercase (e.g. order_date, first_name).
  • Always Quote Text Literals: Never omit single quotes for text values: WHERE role = 'Admin'.
  • Format with One Clause per Line: Place each primary clause on its own line for immediate readability.
  • Terminate with Semicolons: Ensure every complete statement ends with a ; for clean execution scripts.
15

What You Should Know Now

  • SQL Statements: Complete instructions sent to database
  • Keywords vs Identifiers: Built-in commands vs user objects
  • Clauses: Structural units (SELECT, FROM, WHERE)
  • Expressions & Values: Calculated formulas and literal constants
  • Comments: Single-line (--) and multi-line (/* */)
  • Statement Categories: DQL, DML, DDL, and TCL

🎯 Knowledge Check Quiz: Basic SQL Syntax

Test your understanding of SQL tokens, keywords, clauses, statements, and grammar rules.

1. In SQL, what is the fundamental difference between a Keyword and an Identifier?
2. What is a SQL "Clause"?
3. Why must string (text) literals like names and cities be enclosed in single quotation marks in SQL queries?
4. Which of the following represents the correct logical clause order for a basic query?
5. How do you write a single-line comment in standard SQL?
6. Which SQL statement category do INSERT, UPDATE, and DELETE belong to?
7. Given the query: "SELECT first_name AS name FROM staff;", what is "name"?
8. What is the purpose of the semicolon (;) at the end of a SQL statement?