How are SQL statements commonly classified, and what is the purpose of each category?
SQL commands are commonly grouped into DDL, DML, DQL, DCL, and TCL based on their purpose. The categories are a practical classification convention rather than a universal formal division, so database vendors may present them differently.
Read full answerHide full answer
Applies toSQL:2023 (ISO/IEC 9075:2023); command availability varies by database
SQL Command Categories
| Category | Purpose | Common commands |
|---|---|---|
| DDL — Data Definition Language | Defines and changes database objects and their structure | CREATE, ALTER, DROP, TRUNCATE |
| DML — Data Manipulation Language | Changes stored data | INSERT, UPDATE, DELETE, MERGE |
| DQL — Data Query Language | Retrieves data | SELECT |
| DCL — Data Control Language | Manages permissions and access rights | GRANT, REVOKE |
| TCL — Transaction Control Language | Controls transactions and their outcome | COMMIT, ROLLBACK, SAVEPOINT |
Data Definition Language
DDL establishes the structure in which data is stored.
-
CREATE TABLEdefines a table, its columns, constraints, and related objects. -
ALTER TABLEmodifies an existing table, such as adding a column or changing a constraint. -
DROP TABLEpermanently removes a table and its structure. -
TRUNCATE TABLEremoves many rows efficiently while normally preserving the table structure. -
CREATE INDEX,CREATE VIEW, andCREATE SCHEMAdefine other kinds of database objects.
DDL operations usually cause significant metadata changes and may require elevated privileges. Exact behavior, especially for DROP and transactional DDL, depends on the database.
Data Manipulation and Query Languages
DML modifies existing rows:
-
INSERTadds new rows. -
UPDATEchanges values in existing rows. -
DELETEremoves rows. -
MERGEconditionally inserts, updates, or deletes rows, commonly from a source relation.
DQL retrieves information through SELECT. It is often taught separately from DML even though some classifications include querying within DML.
A query can filter rows with WHERE, group results with GROUP BY, filter groups with HAVING, order output with ORDER BY, and limit results with FETCH FIRST or OFFSET. Window functions such as ROW_NUMBER() and SUM() OVER perform calculations across related rows without collapsing them into groups.
SELECT without a WHERE clause does not necessarily return every row, and aggregate functions such as SUM() and COUNT() ignore NULL values unless the query explicitly handles them.
Access Control and Transaction Control
DCL controls who can access database objects or perform operations:
-
GRANTgives a privilege to a user or role. -
REVOKEremoves a previously granted privilege. -
CREATE ROLEandDROP ROLEmanage roles in databases that support them.
TCL controls transaction boundaries and outcomes:
-
COMMITmakes the transaction’s changes permanent. -
ROLLBACKundoes the transaction’s uncommitted changes. -
SAVEPOINTcreates a named point to which a transaction can be rolled back partially.
Isolation is configured separately using settings such as READ COMMITTED, REPEATABLE READ, or SERIALIZABLE. PostgreSQL supports all four standard SQL isolation levels, while MySQL and SQL Server have vendor-specific restrictions or differences, particularly for READ UNCOMMITTED and SERIALIZABLE.
Vendor-Specific and Utility Commands
Databases also provide commands outside these five core categories:
-
Data utilities:
ANALYZE,VACUUM,OPTIMIZE, andREINDEXmaintain or optimize stored data and indexes. -
Execution and diagnostics:
EXPLAINshows a query plan in PostgreSQL, MySQL, and SQL Server, though each has different options and output. -
Procedural and session control: Commands for variables, cursors, procedures, sessions, and connection settings vary substantially between products.
For example, PostgreSQL uses VACUUM for routine table maintenance, MySQL commonly uses OPTIMIZE TABLE, and SQL Server uses ALTER INDEX ... REBUILD. These are not interchangeable even when they address a similar maintenance goal.