SQL Syntax & Statement Types
1. Introduction to SQL Syntax
Every time you write a query, you’re using a specific syntax — a set of rules that define how SQL commands must be structured. Just like grammar in English, SQL has its own structure: keywords, clauses, punctuation (like commas and semicolons), and naming conventions. Understanding SQL syntax ensures your commands are valid and readable across different database systems.
Examples of syntax rules include:
- Each SQL statement ends with a semicolon (;) in many systems (though some allow omission).
- Keywords are often not case-sensitive (e.g., SELECT is same as select), but for readability most use uppercase.
- Identifiers (table names, column names) should not conflict with reserved words.
- String values usually need to be enclosed in quotes (‘ or “) depending on the system.
2. Key SQL Statement Types
2.1 DDL – Data Definition Language
These statements define or alter the structure of your database.
- CREATE — create tables, views, indexes.
- ALTER — modify an existing table (add/remove columns).
- DROP — remove a table or other structure.
Example:
CREATE TABLE Departments (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) NOT NULL
);
2.2 DML – Data Manipulation Language
These statements work with the data inside your tables.
- INSERT — add rows.
- UPDATE — change existing rows.
- DELETE — remove rows.
Example:
INSERT INTO Departments (DeptID, DeptName)
VALUES (10, 'Research');
2.3 DQL – Data Query Language
Primarily the SELECT statement. It retrieves data.
Example:
SELECT DeptName
FROM Departments;
2.4 DCL & TCL – Control & Transaction Language
- DCL (Data Control Language): GRANT, REVOKE — control permissions.
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT — manage transactions and changes.
Example:
BEGIN TRANSACTION;
UPDATE Departments
SET DeptName = 'R&D'
WHERE DeptID = 10;
COMMIT;
3. Basic Syntax Components & Rules
3.1 Keywords and Case
Although SQL keywords are usually case-insensitive, the convention is to write them in UPPERCASE to improve readability: SELECT, FROM, WHERE. Table and column names can be mixed-case or lower case, depending on naming convention (e.g., deptName or DeptName).
3.2 Semicolon (;)
Many SQL systems require a semicolon at the end of a statement, especially when running multiple commands in one batch.
SELECT * FROM Departments;
3.3 Identifiers (names of tables/columns)
- Must start with a letter (depends on system).
- Cannot be a reserved keyword (like SELECT, TABLE).
- If they include spaces or special characters, they often must be quoted (for example [Employee Name] in SQL Server).
- Should follow a naming convention (e.g., snake_case, camelCase) for consistency.
3.4 String Literals & Dates
- Strings are enclosed in single quotes: ‘London’.
- Dates may depend on DB system: ‘2025-11-14’, or use special functions like DATE ‘2025-11-14’.
- Numeric values don’t need quotes.
3.5 Comments
Use comments to annotate your code:
- Single line: — This is a comment
- Multi line (in many systems): /* comment block */
4. A Combined Example
Let’s walk through a full mini-scenario:
-- Create a table of Projects
CREATE TABLE Projects (
ProjectID INT PRIMARY KEY,
ProjectName VARCHAR(100) NOT NULL,
StartDate DATE,
Budget DECIMAL(12,2)
);
-- Insert some sample data
INSERT INTO Projects (ProjectID, ProjectName, StartDate, Budget)
VALUES (1, 'Website Upgrade', '2025-10-01', 15000.00),
(2, 'Mobile App Launch', '2025-09-15', 32000.00),
(3, 'Data Warehouse Setup', '2025-11-01', 45000.00);
-- Query the table
SELECT ProjectID, ProjectName, Budget
FROM Projects
WHERE Budget > 20000
ORDER BY Budget DESC;
Explanation:
- We defined the table structure (DDL).
- We inserted rows (DML).
- We retrieved data with conditions and ordering (DQL).
- We used proper syntax rules: uppercase keywords, semicolon at end, comments.
5. Best Practices for Syntax & Readability
- Always indent and break long queries into multiple lines for readability.
- Use meaningful table and column names (e.g., StartDate rather than SD).
- Keep consistent naming style across tables (e.g., all singular or all plural).
- Use aliases (AS) when joining tables for clarity.
- Avoid SELECT * in production code—explicitly list needed columns.
- Comment your SQL so that others (and future you!) will understand the intent.
- Use transactions when performing multiple changes that must succeed or fail as a unit.
6. Summary
In this tutorial you learned:
- SQL syntax is the set of rules and structure you follow when writing statements.
- SQL statements fall into types: DDL, DML, DQL, DCL/TCL.
- Key syntax elements: keywords, identifiers, literals, semicolons, comments.
- A combined example showed how structure, data manipulation, and querying fit together.
- Readability and best practices matter just as much as correctness.