Types of SQL Commands

Five Command Families

SQL commands are grouped by what they do. Interviewers often ask for these categories.

GroupFull nameCommandsPurpose
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATE, RENAMEdefine and change the structure (schema) of tables, views and indexes
DQLData Query LanguageSELECTread data
DMLData Manipulation LanguageINSERT, UPDATE, DELETE, MERGEchange the data inside tables
DCLData Control LanguageGRANT, REVOKEgive or take away permissions
TCLTransaction Control LanguageCOMMIT, ROLLBACK, SAVEPOINTmanage 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.