SQL Syntax Basics – Complete Beginner's Guide with Examples (2026-27)
SQL syntax is the set of rules that defines how SQL statements are written and structured. Understanding SQL syntax is the essential second step after learning what SQL is — because even a small syntax error will prevent your query from running. In this beginner-friendly guide you will learn all the SQL syntax rules, the correct clause order, all major SQL commands with syntax and examples (SELECT, INSERT, UPDATE, DELETE, CREATE, DROP), SQL data types, how to write comments, and the most common syntax mistakes to avoid.
✅ What is SQL Syntax?
SQL syntax is the grammar of SQL — the set of rules that defines how SQL statements must be written for the database to understand and execute them correctly. Just like English sentences follow grammar rules, SQL statements must follow syntax rules.
✅ A valid SQL statement has a specific structure, keyword order, and punctuation.
✅ SQL syntax is mostly the same across all databases (MySQL, PostgreSQL, Oracle, SQL Server), with small differences in advanced features.
✅ SQL is not case sensitive for keywords — SELECT, select, and Select are all valid. However, writing keywords in UPPERCASE is the standard convention that makes queries easier to read.
SELECT, FROM, WHERE) and table/column names in the case they were created. This is not required but is the widely accepted best practice.
✅ Core SQL Syntax Rules Every Beginner Must Know
✅ Rule 1 – End statements with a semicolon
Every SQL statement ends with a ; (semicolon). This tells the database engine where one statement ends and the next begins.
SELECT * FROM Employees;
SELECT * FROM Products;
✅ Rule 2 – SQL keywords are NOT case sensitive
SELECT, select, and Select all work the same. But always write keywords in UPPERCASE for readability.
✅ Rule 3 – Text values need single quotes
String (text) values in conditions must be wrapped in single quotes. Numbers do NOT need quotes.
-- Correct: text in single quotes
WHERE Department = 'IT'
-- Correct: numbers without quotes
WHERE Salary > 5000
✅ Rule 4 – Whitespace and line breaks are ignored
You can split a SQL query across multiple lines to improve readability. Extra spaces and line breaks are ignored by the database.
-- These two queries are identical:
SELECT Name, Salary FROM Employees WHERE Department = 'IT';
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT';
✅ Rule 5 – Identifiers (table/column names) are case-sensitive in some databases
Table and column names follow the case they were created with. In PostgreSQL, unquoted identifiers are lowercased automatically. In MySQL, table names are case-sensitive on Linux but not on Windows.
✅ Rule 6 – NULL is not a value — use IS NULL / IS NOT NULL
You cannot compare NULL with = or !=. Always use IS NULL or IS NOT NULL.
-- Wrong — returns 0 rows always:
WHERE Email = NULL
-- Correct:
WHERE Email IS NULL
✅ Correct Clause Order in a SQL SELECT Statement
SQL clauses must be written in a strict order. Writing them out of sequence causes a syntax error.
| # | Clause | Purpose | Required? |
|---|---|---|---|
| 1 | SELECT | Specify columns to retrieve | ✅ Yes |
| 2 | FROM | Specify the table | ✅ Yes |
| 3 | WHERE | Filter rows by condition | Optional |
| 4 | GROUP BY | Group rows for aggregation | Optional |
| 5 | HAVING | Filter grouped rows | Optional |
| 6 | ORDER BY | Sort results | Optional |
| 7 | LIMIT | Limit number of rows returned | Optional |
✅ Full SELECT query using all clauses:-
SELECT Department, COUNT(*) AS Total
FROM Employees
WHERE City = 'Pune'
GROUP BY Department
HAVING COUNT(*) > 1
ORDER BY Total DESC
LIMIT 5;
SELECT and FROM are required. All other clauses are optional — but when you include them, they MUST appear in the order shown above.
✅ SQL Commands Overview Table
Here is a quick reference of the most important SQL commands every beginner should know:
| Command | Category | Description | Example |
|---|---|---|---|
SELECT | DQL | Retrieve data from a table | SELECT * FROM Employees; |
INSERT INTO | DML | Add a new row to a table | INSERT INTO Employees VALUES (...); |
UPDATE | DML | Modify existing rows in a table | UPDATE Employees SET Salary = 9000; |
DELETE | DML | Remove rows from a table | DELETE FROM Employees WHERE ID = 3; |
CREATE TABLE | DDL | Create a new table | CREATE TABLE Employees (...); |
ALTER TABLE | DDL | Add or modify columns in a table | ALTER TABLE Employees ADD Email VARCHAR(100); |
DROP TABLE | DDL | Permanently delete a table | DROP TABLE Employees; |
TRUNCATE | DDL | Remove all rows from a table | TRUNCATE TABLE Employees; |
CREATE DATABASE | DDL | Create a new database | CREATE DATABASE CompanyDB; |
✅ SELECT – Read / Retrieve Data Syntax
The SELECT statement retrieves data from a table. It is the most commonly used SQL command. See our full SQL SELECT guide for detailed examples.
✅ Syntax:-
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column ASC|DESC
LIMIT number;
Example 1 — Get all columns from Employees
SELECT * FROM Employees;
Example 2 — Get specific columns
SELECT Name, Department, Salary
FROM Employees;
Example 3 — Filter with WHERE
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
ORDER BY Salary DESC;
✅ Result — IT employees sorted by salary:-
| Name | Salary |
|---|---|
| ANNI | 10000 |
| POOJA | 9000 |
✅ INSERT INTO – Add New Data Syntax
The INSERT INTO statement adds a new row to a table.
✅ Syntax:-
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example — Add a new employee
INSERT INTO Employees (EmployeeID, Name, Department, Salary, City)
VALUES (6, 'VIKRAM', 'IT', 11000, 'Bangalore');
Example — Insert multiple rows at once (MySQL/PostgreSQL)
INSERT INTO Employees (EmployeeID, Name, Department, Salary, City)
VALUES
(7, 'KAVYA', 'HR', 7500, 'Chennai'),
(8, 'ARJUN', 'Sales', 6000, 'Hyderabad'),
(9, 'PRIYA', 'IT', 9500, 'Pune');
✅ UPDATE – Modify Existing Data Syntax
The UPDATE statement changes values in existing rows. It always works together with a WHERE clause to target specific rows.
✅ Syntax:-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example 1 — Update one column
UPDATE Employees
SET Salary = 12000
WHERE Name = 'POOJA';
Example 2 — Update multiple columns
UPDATE Employees
SET Salary = 15000, City = 'Mumbai'
WHERE EmployeeID = 1;
Example 3 — Update all rows in a department
UPDATE Employees
SET Salary = Salary + 1000
WHERE Department = 'Sales';
UPDATE Employees SET Salary = 5000; — this updates every single row in the table. Always include a WHERE condition. Before running UPDATE, test your WHERE clause with a SELECT first.
✅ DELETE – Remove Data Syntax
The DELETE statement removes rows from a table based on a condition.
✅ Syntax:-
DELETE FROM table_name
WHERE condition;
Example 1 — Delete one specific row
DELETE FROM Employees
WHERE EmployeeID = 3;
Example 2 — Delete all rows from a department
DELETE FROM Employees
WHERE Department = 'Sales';
DELETE FROM Employees; — this permanently deletes every row in the table. The data cannot be recovered without a backup. Always specify a WHERE condition.
DELETE FROM Employees WHERE Department = 'Sales' — removes matching rows, can be rolled back.TRUNCATE TABLE Employees — removes ALL rows instantly, cannot be rolled back in most databases. Use only when you want to completely empty a table.
✅ CREATE TABLE – Create a New Table Syntax
The CREATE TABLE statement defines a new table, its columns, and data types.
✅ Syntax:-
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
Example — Create the Employees table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Department VARCHAR(50),
Salary DECIMAL(10,2),
City VARCHAR(50),
HireDate DATE
);
PRIMARY KEY — unique identifier for each rowNOT NULL — column cannot be emptyUNIQUE — all values in this column must be differentDEFAULT value — use this value if no value is providedFOREIGN KEY — links this column to a primary key in another table
✅ DROP TABLE – Delete a Table Syntax
The DROP TABLE statement permanently deletes a table and all its data from the database. This action cannot be undone.
✅ Syntax:-
DROP TABLE table_name;
Example
DROP TABLE Employees;
Safe version — only drops if the table exists
DROP TABLE IF EXISTS Employees;
✅ SQL Data Types Reference Table
Every column in a SQL table must have a data type that defines what kind of value it can store. Here are the most common SQL data types:
| Category | Data Type | Description | Example Value |
|---|---|---|---|
| Numeric | INT | Whole numbers (no decimal) | 1, 42, 10000 |
DECIMAL(p,s) | Exact decimal number with precision p and scale s | 9500.50 | |
FLOAT | Approximate decimal number | 3.14159 | |
| Text | VARCHAR(n) | Variable-length string up to n characters | 'Pramod', 'IT' |
CHAR(n) | Fixed-length string of exactly n characters | 'M', 'IN' | |
TEXT | Long text of unlimited length | Article body | |
| Date/Time | DATE | Date only (YYYY-MM-DD) | 2026-05-26 |
DATETIME | Date and time (YYYY-MM-DD HH:MM:SS) | 2026-05-26 14:30:00 | |
TIMESTAMP | Date and time, auto-updates on change | 2026-05-26 14:30:00 | |
| Boolean | BOOLEAN | True or False | TRUE, FALSE |
VARCHAR(n) for names and text, INT for IDs and counts, DECIMAL(10,2) for money/salary, and DATE for dates. These four cover the vast majority of real-world use cases.
✅ Complete Practice Example – From Table Creation to Query
Here is a complete end-to-end SQL example covering CREATE, INSERT, SELECT, UPDATE, and DELETE — all in one sequence. This is how real database work flows:
Step 1 — Create the Employees tableCREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Department VARCHAR(50),
Salary DECIMAL(10,2),
City VARCHAR(50)
);
Step 2 — Insert sample data
INSERT INTO Employees (EmployeeID, Name, Department, Salary, City)
VALUES
(1, 'ANNI', 'IT', 10000, 'Mumbai'),
(2, 'POOJA', 'IT', 9000, 'Pune'),
(3, 'RAJ', 'SALES', 5000, 'Delhi'),
(4, 'SUJAN', 'HR', 8000, 'Pune'),
(5, 'MEENA', 'SALES', 12000, 'Mumbai');
Step 3 — Read all data
SELECT * FROM Employees;
✅ Result:-
| EmployeeID | Name | Department | Salary | City |
|---|---|---|---|---|
| 1 | ANNI | IT | 10000.00 | Mumbai |
| 2 | POOJA | IT | 9000.00 | Pune |
| 3 | RAJ | SALES | 5000.00 | Delhi |
| 4 | SUJAN | HR | 8000.00 | Pune |
| 5 | MEENA | SALES | 12000.00 | Mumbai |
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
AND Salary > 9000
ORDER BY Salary DESC;
✅ Result:-
| Name | Salary |
|---|---|
| ANNI | 10000.00 |
UPDATE Employees
SET Salary = 11000
WHERE Name = 'POOJA';
Step 6 — Remove RAJ from the database
DELETE FROM Employees
WHERE EmployeeID = 3;
Step 7 — Verify final state
SELECT * FROM Employees
ORDER BY EmployeeID;
✅ Common SQL Syntax Mistakes to Avoid
Every SQL statement should end with
;. Without it, some tools will throw an error or run multiple statements incorrectly.
Wrong:
SELECT * FROM Employees ORDER BY Name WHERE Department = 'IT';Correct:
SELECT * FROM Employees WHERE Department = 'IT' ORDER BY Name;Clause order must be: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.
Wrong:
WHERE Department = ITCorrect:
WHERE Department = 'IT'All text/string values must be in single quotes.
Wrong:
WHERE Email = NULL — always returns 0 rows.Correct:
WHERE Email IS NULL
Without a WHERE clause, UPDATE changes ALL rows and DELETE removes ALL rows permanently. Always include a WHERE condition and test it with SELECT first.
Wrong:
WHERE Name = "ANNI" — double quotes are for identifiers in SQL, not string values.Correct:
WHERE Name = 'ANNI'
✅ Frequently Asked Questions (FAQ)
-- for a single-line comment: -- This is a comment. Use /* ... */ for multi-line comments. Comments are completely ignored by the database engine and are for human readers only.
✅ Writing SQL Comments
SQL comments are notes in your code that are ignored by the database engine. Use them to explain your queries — especially useful in shared or complex scripts.
✅ Single-line comment — use
--:-✅ Multi-line comment — use
/* ... */:-