Types of SQL Commands-tutorial
SQL (Structured Query Language) is used to manage and interact with databases.
But all SQL commands are not the same they serve different purposes.
So, SQL commands are categorized into five main types, each handling a specific part of database operations.
What Are SQL Commands?
SQL commands are instructions you give to a database.
They tell the database what to do like creating a table, inserting data, or giving permission to users.
1. DDL – Data Definition Language
Purpose: Defines or changes the structure of the database (like tables, columns, etc.)
| Command | Description | Example |
|---|---|---|
| CREATE | Creates a new database object (table, view, etc.) | CREATE TABLE Students (ID INT, Name VARCHAR (50)); |
| ALTER | Modifies an existing table structure | ALTER TABLE Stdents ADD Age INT; |
| DROP | Deletes an existing database object | DROP TABLE Students; |
| TRUNCATE | Removes all data from a table but keeps structure | TRUNCATE TABLE Students; |
DDL commands auto-save changes, so they cannot be rolled back easily.
2. DML – Data Manipulation Language
Purpose: Used to manipulate or manage data inside tables.
| Command | Description | Example |
|---|---|---|
| INSERT | Adds new records | INSERT INTO Students VALUES (1, 'Amit', 22); |
| UPDATE | Modifies existing records | UPDATE Students SET Age = 23 WHERE ID = 1; |
| DELETE | Removes records | DELETE FROM Students WHERE ID = 1; |
| SELECT | Retrieves data from tables | SELECT * FROM Students; |
DML commands can be rolled back if changes are not committed.
3. DCL – Data Control Language
Purpose: Controls access and permissions to the database.
Note: Some databases consider SELECT under DML, but it is often treated as a separate DQL category.
| Command | Description | Example |
|---|---|---|
| GRANT | Gives permission to users | GRANT SELECT ON Students TO user1; |
| REVOKE | Takes back permission | REVOKE SELECT ON Students FROM user1; |
Used mostly by database administrators for security control.
4. TCL – Transaction Control Language
Purpose: Manages changes made by DML commands and controls transactions.
| Command | Description | Example |
|---|---|---|
| COMMIT | Saves all change permanently | COMMIT; |
| ROLLBACK | Undo uncommitted changes | ROLLBACK; |
| SAVEPOINT | Creates a poin to rollback partially | SAVEPOINT save1; |
TCL ensures data consistency and prevents data loss during transactions.
5. DQL – Data Query Language
Purpose: Retrieves data from the database.
It mainly includes the SELECT command.
SELECT Name, Age FROM Students WHERE Age > 20;Note: Some databases consider SELECT under DML, but it is often treated as a separate DQL category.
Two Minute Drill
- DDL - Structure Commands (CREATE, ALTER, DROP)
- DML - Data commands (INSERT, UPDATE, DELETE, SELECT)
- DCL - Security commands (GRANT, REVOKE)
- TCL - Transaction commands (COMMIT, ROLLBACK, SAVEPOINT)
- DQL - Query command (SELECT)