SQL Command Categories (DDL, DML, DQL, DCL, TCL)
Group SQL statements into DDL, DML, DQL, DCL, and TCL with clear mental models.
In plain words
Some commands build tables, some change the data in them, some read it, some give people permission, and some save or undo a batch of changes.
Think of it like…
Running a library: DDL builds or removes shelves, DML adds, edits or removes books, DQL is reading, DCL hands out library cards with different access, and TCL is deciding whether today's changes to the catalogue are final or should be undone.
Where you’ve seen it
A release adds a column (DDL: ALTER TABLE), backfills it (DML: UPDATE), grants the reporting role access (DCL: GRANT), all wrapped in BEGIN … COMMIT (TCL). In PostgreSQL the whole migration can be rolled back if one step fails; in MySQL the ALTER commits immediately.
Words you’ll hear
- DDL
- CREATE, ALTER, DROP, TRUNCATE, RENAME: change the schema.
- DML / DQL
- INSERT, UPDATE, DELETE, MERGE / SELECT: change or read rows.
- DCL
- GRANT, REVOKE: control who can do what.
- TCL
- BEGIN, COMMIT, ROLLBACK, SAVEPOINT: control transactions.
Visual diagram
Theory & example queries
What each category does
DDL defines and changes database objects: tables, indexes, views, schemas. DML changes the rows inside tables, and DQL (SELECT) reads them; some textbooks fold SELECT into DML, so mention both conventions. DCL grants and revokes privileges on objects to users and roles.
TCL controls transactions: BEGIN (or START TRANSACTION) opens one, COMMIT makes its changes permanent, ROLLBACK undoes them, and SAVEPOINT marks a point you can roll back to without abandoning the whole transaction.
CREATE TABLE notes (id bigint PRIMARY KEY, body text);PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
INSERT INTO notesVALUES (1, 'hi');PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
SELECT *FROM notes;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
GRANTSELECT ON notes TO analyst;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
COMMIT;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Representative sequence
Note: Command tags and COMMIT behavior vary by client and dialect; the SELECT result reflects the single inserted row.
(DDL) ·
(DML) ·
(DQL) ·
(DCL) ·
(TCL)
Key takeaway: Structure, data, reads, permissions, transactions.
Why the category matters in practice
DDL usually takes strong locks on the table it changes, so an ALTER on a busy table can block every query behind it. Large DDL in production is planned work: off-peak, with lock timeouts, or using online techniques such as CREATE INDEX CONCURRENTLY.
Transaction behaviour differs by database. PostgreSQL supports transactional DDL, so a failed migration can be rolled back entirely. MySQL and Oracle commit implicitly before and after most DDL statements, so a migration that fails halfway leaves earlier steps applied. TRUNCATE follows the same DDL rules.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
BEGIN;SET LOCAL lock_timeout = '5s';ALTER TABLE ordersADD COLUMN source text;UPDATE ordersSET source = 'web'WHERE source IS NULL;COMMIT;PostgreSQL is the lesson baseline unless noted otherwise.
Open a transaction so changes can be checked and committed or rolled back together.
Representative PostgreSQL transaction
Note: The 120-row update count is illustrative. PostgreSQL can roll back the entire sequence if a step fails before COMMIT.
If any step fails, nothing is applied.
Key takeaway: DDL locks hard and, outside PostgreSQL, commits on its own.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Define the structure (DDL)
Create tables, indexes and constraints, and change them later with ALTER.
CREATE TABLE … /ALTER TABLE …When to use it & trade-offs
What it is
SQL statements fall into five groups. DDL (Data Definition) changes structure: CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation) changes rows: INSERT, UPDATE, DELETE, MERGE. DQL (Data Query) reads: SELECT. DCL (Data Control) manages permissions: GRANT, REVOKE. TCL (Transaction Control) groups changes: BEGIN, COMMIT, ROLLBACK, SAVEPOINT.
Why it matters: It's a classic fresher interview question, and the categories have real consequences: DDL takes stronger locks and, in MySQL and Oracle, commits the open transaction automatically, while DML can be rolled back. Knowing which group a command belongs to tells you how risky it is to run in production.
When to reach for it
- Answering "what are DDL, DML, DCL and TCL?"
- Planning database migrations and releases
- Setting up roles and permissions
When to avoid or be careful
- Running ALTER TABLE on a busy production table without a lock timeout
- Assuming DDL can be rolled back in MySQL
- Giving application users DDL privileges they don't need
How the categories behave
Strengths: Defines structure and constraints the database enforces.
Watch out: Strong locks; implicit commits in MySQL and Oracle.
Strengths: Row-level locking; fully transactional.
Watch out: Large DML (millions of rows) still needs batching.
Strengths: Least privilege and atomic changes.
Watch out: Forgetting COMMIT, or leaving transactions open, causes subtle problems.
Interview signals & numbers to know
When this topic comes up
- “Difference between DDL and DML”
- “Is TRUNCATE DDL or DML?”
- “Can you roll back a CREATE TABLE?”
- Database migrations
- GRANT / REVOKE
Benchmarks & rules of thumb
- 5
- categories: DDL, DML, DQL, DCL, TCL
- TRUNCATE
- DDL
- PostgreSQL
- supports transactional DDL
Common mistakes & follow-ups
Common interview mistakes
- Calling TRUNCATE a DML command.
- Expecting a failed MySQL migration to roll back its earlier ALTER statements.
- Forgetting to COMMIT in a client with autocommit turned off, so changes vanish when the session closes.
Likely interviewer follow-ups
- Why is TRUNCATE considered DDL?
- Can DDL be rolled back? Does it depend on the database?
- Which category does SELECT belong to, and why do sources disagree?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply SQL Command Categories (DDL, DML, DQL, DCL, TCL) with hands-on SQL practice and interview Q&A.