Structured Query Language (SQL) is the standard language used to communicate with and manage relational databases. Whether you are storing, retrieving, or updating data, SQL provides a set of commands and rules (syntax) to perform these tasks efficiently.
In this guide, we will explore the SQL syntax in detail, with examples for each part, so you can understand and write SQL queries confidently.
1. Basic Structure of SQL Syntax
Every SQL statement generally follows this format:
KEYWORD arguments;
- Keywords: These are predefined commands such as
SELECT
,INSERT
,UPDATE
,DELETE
. - Arguments: These can be table names, column names, values, or conditions.
- Semicolon (;): Marks the end of the SQL statement.
Example:
SELECT name, age FROM students;
This statement retrieves the name
and age
columns from the students
table.
2. SQL is Case-Insensitive
SQL keywords are not case-sensitive. This means:
select name from students;
and
SELECT name FROM students;
will work the same.
However, by convention, keywords are written in uppercase for better readability.
3. SQL Syntax Categories
The SQL syntax is divided into different command categories:
3.1 Data Query Language (DQL)
Used to retrieve data.
- Main Command:
SELECT
Example:
SELECT * FROM employees;
Retrieves all columns from the employees
table.
3.2 Data Definition Language (DDL)
Used to define and modify database structures.
- Commands:
CREATE
,ALTER
,DROP
,TRUNCATE
Example – Create a Table:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Example – Alter Table:
ALTER TABLE students ADD email VARCHAR(100);
3.3 Data Manipulation Language (DML)
Used to insert, update, and delete data.
- Commands:
INSERT
,UPDATE
,DELETE
Example – Insert Data:
INSERT INTO students (id, name, age) VALUES (1, 'Amit', 20);
Example – Update Data:
UPDATE students SET age = 21 WHERE id = 1;
Example – Delete Data:
sqlCopyEditDELETE FROM students WHERE id = 1;
3.4 Data Control Language (DCL)
Used to control access to data.
- Commands:
GRANT
,REVOKE
Example:
GRANT SELECT ON students TO user1;
3.5 Transaction Control Language (TCL)
Manages transactions in a database.
- Commands:
COMMIT
,ROLLBACK
,SAVEPOINT
Example:
BEGIN;
UPDATE students SET age = 22 WHERE id = 2;
COMMIT;
4. SELECT Statement Syntax
The SELECT
statement is the most used SQL command. Basic syntax:
SELECT column1, column2 FROM table_name WHERE condition;
Example 1 – Selecting Specific Columns
SELECT name, age FROM students;
Example 2 – Selecting All Columns
SELECT * FROM students;
Example 3 – Adding Conditions
SELECT * FROM students WHERE age > 18;
5. WHERE Clause
The WHERE
clause filters records based on a condition.
Operators:
=
Equal>
Greater than<
Less than>=
Greater than or equal<=
Less than or equal<>
or!=
Not equal
Example:
SELECT * FROM employees WHERE department = 'HR';
6. ORDER BY Clause
Sorts data in ascending (ASC
) or descending (DESC
) order.
Example:
SELECT name, age FROM students ORDER BY age DESC;
7. GROUP BY and HAVING
Used for grouping rows and filtering groups.
Example:
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
8. SQL Joins Syntax
Joins combine data from multiple tables.
INNER JOIN
SELECT students.name, courses.course_name
FROM students
INNER JOIN courses ON students.course_id = courses.id;
LEFT JOIN
SELECT students.name, courses.course_name
FROM students
LEFT JOIN courses ON students.course_id = courses.id;
RIGHT JOIN
SELECT students.name, courses.course_name
FROM students
RIGHT JOIN courses ON students.course_id = courses.id;
9. INSERT Syntax
Used to add new rows.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Example:
INSERT INTO students (name, age) VALUES ('Riya', 19);
10. UPDATE Syntax
Used to modify existing records.
UPDATE table_name SET column1 = value1 WHERE condition;
Example:
UPDATE students SET age = 20 WHERE name = 'Riya';
11. DELETE Syntax
Removes records.
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM students WHERE age < 18;
12. CREATE DATABASE Syntax
CREATE DATABASE school;
13. DROP and TRUNCATE Syntax
- DROP: Removes table/database completely.
DROP TABLE students;
- TRUNCATE: Deletes all rows but keeps structure.
TRUNCATE TABLE students;
14. SQL Constraints
Constraints are rules applied to columns:
- PRIMARY KEY
- FOREIGN KEY
- UNIQUE
- NOT NULL
- CHECK
- DEFAULT
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age >= 18),
salary DECIMAL DEFAULT 30000
);
15. Best Practices for Writing SQL
- Use uppercase for keywords (e.g., SELECT, FROM, WHERE).
- Indent and format queries for readability.
- Always use WHERE with
UPDATE
orDELETE
to avoid accidental changes. - Use LIMIT to test queries before running on full datasets.
- Keep table and column names meaningful.
Conclusion
SQL syntax forms the foundation for working with databases. By understanding how to write commands like SELECT
, INSERT
, UPDATE
, and using clauses like WHERE
, ORDER BY
, and GROUP BY
, you can effectively manage and analyze data.
Mastering SQL requires practice — start with small queries and gradually work toward more complex joins and condition.