SQL Tutorial – Lesson 2

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.

ℹ️ Convention: Write SQL keywords in UPPERCASE (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
💡 Pro Tip: When writing complex queries, build them step by step — start with SELECT and FROM, test it, then add WHERE, then ORDER BY. This makes it easier to spot syntax errors.

✅ 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.

#ClausePurposeRequired?
1SELECTSpecify columns to retrieve✅ Yes
2FROMSpecify the table✅ Yes
3WHEREFilter rows by conditionOptional
4GROUP BYGroup rows for aggregationOptional
5HAVINGFilter grouped rowsOptional
6ORDER BYSort resultsOptional
7LIMITLimit number of rows returnedOptional

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;
⚠️ Remember: Only 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:

CommandCategoryDescriptionExample
SELECTDQLRetrieve data from a tableSELECT * FROM Employees;
INSERT INTODMLAdd a new row to a tableINSERT INTO Employees VALUES (...);
UPDATEDMLModify existing rows in a tableUPDATE Employees SET Salary = 9000;
DELETEDMLRemove rows from a tableDELETE FROM Employees WHERE ID = 3;
CREATE TABLEDDLCreate a new tableCREATE TABLE Employees (...);
ALTER TABLEDDLAdd or modify columns in a tableALTER TABLE Employees ADD Email VARCHAR(100);
DROP TABLEDDLPermanently delete a tableDROP TABLE Employees;
TRUNCATEDDLRemove all rows from a tableTRUNCATE TABLE Employees;
CREATE DATABASEDDLCreate a new databaseCREATE 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:-

Result
NameSalary
ANNI10000
POOJA9000

✅ 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');
💡 Best practice: Always list column names explicitly in your INSERT statement. This protects your query from breaking if the table structure is changed later (e.g. a new column is added).

✅ 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';
❌ DANGER: Never run UPDATE without WHERE!
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';
❌ DANGER: Never run DELETE without WHERE!
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 vs TRUNCATE: 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
);
💡 Common constraints:
PRIMARY KEY — unique identifier for each row
NOT NULL — column cannot be empty
UNIQUE — all values in this column must be different
DEFAULT value — use this value if no value is provided
FOREIGN 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;
⚠️ Warning: DROP TABLE permanently deletes both the table structure AND all data inside it. Unlike DELETE (which removes rows), DROP removes the entire table. Always take a backup before running DROP TABLE in production.

✅ 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:

CategoryData TypeDescriptionExample Value
NumericINTWhole numbers (no decimal)1, 42, 10000
DECIMAL(p,s)Exact decimal number with precision p and scale s9500.50
FLOATApproximate decimal number3.14159
TextVARCHAR(n)Variable-length string up to n characters'Pramod', 'IT'
CHAR(n)Fixed-length string of exactly n characters'M', 'IN'
TEXTLong text of unlimited lengthArticle body
Date/TimeDATEDate only (YYYY-MM-DD)2026-05-26
DATETIMEDate and time (YYYY-MM-DD HH:MM:SS)2026-05-26 14:30:00
TIMESTAMPDate and time, auto-updates on change2026-05-26 14:30:00
BooleanBOOLEANTrue or FalseTRUE, FALSE
💡 Beginner tip: Use 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.

✅ 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 --:-

-- This query selects all IT department employees
SELECT * FROM Employees
WHERE Department = 'IT'; -- Only IT rows

Multi-line comment — use /* ... */:-

/*
  Author: Pramod Behera
  Date: 2026-05-26
  Description: Retrieves all active employees in IT with salary above 8000
*/
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
  AND Salary > 8000;
ℹ️ When to use comments: Add comments when writing complex queries, explaining business logic, noting why a specific condition is used, or documenting a query you will share with your team. Good comments make SQL code maintainable.

✅ 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 table
CREATE 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:-

Employees
EmployeeIDNameDepartmentSalaryCity
1ANNIIT10000.00Mumbai
2POOJAIT9000.00Pune
3RAJSALES5000.00Delhi
4SUJANHR8000.00Pune
5MEENASALES12000.00Mumbai
Step 4 — Filter IT employees earning more than 9000
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
  AND Salary > 9000
ORDER BY Salary DESC;

Result:-

NameSalary
ANNI10000.00
Step 5 — Give POOJA a raise
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;
💡 Practice tip: Run this complete example in a free online SQL editor like SQLFiddle, DB Fiddle, or W3Schools SQL Tryit Editor to see the results live without installing anything.

✅ Common SQL Syntax Mistakes to Avoid

❌ Mistake 1: Missing semicolon at the end
Every SQL statement should end with ;. Without it, some tools will throw an error or run multiple statements incorrectly.
❌ Mistake 2: Wrong clause order
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.
❌ Mistake 3: Forgetting single quotes around text values
Wrong: WHERE Department = IT
Correct: WHERE Department = 'IT'
All text/string values must be in single quotes.
❌ Mistake 4: Using = NULL instead of IS NULL
Wrong: WHERE Email = NULL — always returns 0 rows.
Correct: WHERE Email IS NULL
❌ Mistake 5: UPDATE or DELETE without WHERE
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.
❌ Mistake 6: Using double quotes for string values
Wrong: WHERE Name = "ANNI" — double quotes are for identifiers in SQL, not string values.
Correct: WHERE Name = 'ANNI'

✅ Frequently Asked Questions (FAQ)

What is SQL syntax?
SQL syntax is the set of rules that defines how SQL statements are written and structured. It includes rules about keywords, clause order, punctuation, string quoting, and formatting that the database engine uses to interpret your queries correctly.
Is SQL case sensitive?
SQL keywords like SELECT, FROM, WHERE are NOT case sensitive — SELECT and select work the same. However, string values in WHERE conditions may be case sensitive depending on the database. MySQL is case-insensitive for strings by default; PostgreSQL is case-sensitive. Writing keywords in UPPERCASE is the standard convention.
Do I need a semicolon at the end of SQL statements?
A semicolon (;) at the end of a SQL statement is best practice and required when running multiple statements. Some tools allow you to omit it for a single query, but always include it to avoid errors and follow standard SQL conventions.
What is the correct order of SQL clauses?
The correct order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Only SELECT and FROM are required. All other clauses are optional but must appear in this order when used.
How do I write comments in SQL?
Use -- 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.
What are the main SQL data types?
The main SQL data types are: INT (whole numbers), DECIMAL/FLOAT (decimal numbers), VARCHAR(n) (variable-length text), CHAR(n) (fixed-length text), DATE (YYYY-MM-DD), DATETIME (date and time), and BOOLEAN (true/false). Use VARCHAR for names, INT for IDs, DECIMAL for money, and DATE for dates.


✍️ About the Author: Pramod Behera

Pramod Behera is a SAP and SQL educator with 10+ years of experience in enterprise software and database systems. He founded LearnToSAP.com to help beginners learn SAP and SQL concepts through clear, practical, real-world examples. His tutorials have helped thousands of students and IT professionals across India and beyond.