Careeroza
Types of SQL Commands
Five Command Families
SQL commands are grouped by what they do. Interviewers often ask for these categories.
| Group | Full name | Commands | Purpose |
|---|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE, RENAME | define and change the structure (schema) of tables, views and indexes |
| DQL | Data Query Language | SELECT | read data |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE, MERGE | change the data inside tables |
| DCL | Data Control Language | GRANT, REVOKE | give or take away permissions |
| TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT | manage transactions |
One Example of Each
-- DDL: create a table
CREATE TABLE Shippers (ShipperID INT PRIMARY KEY, ShipperName VARCHAR(50));
-- DML: add a row
INSERT INTO Shippers (ShipperID, ShipperName) VALUES (1, 'Speedy Express');
-- DQL: read it back
SELECT * FROM Shippers;
-- DCL: let a reporting user read it
GRANT SELECT ON Shippers TO report_user;
-- TCL: make the changes permanent
COMMIT;Auto-commit vs Transactions
DDL statements are usually auto-committed in MySQL and Oracle: they cannot be rolled back. PostgreSQL and SQL Server can run most DDL inside a transaction. DML changes can always be grouped in a transaction and undone with ROLLBACK until you COMMIT.
Tip: TRUNCATE is DDL while DELETE is DML. That is why TRUNCATE is faster but, in many databases, cannot be rolled back.