# Topic 3: SQL Command Types
The 5 major SQL sub-languages: DDL, DML, DQL, DCL, and TCL with practical syntax and execution context.
1. Definition
SQL commands are categorized into five sub-languages based on their function: DDL (structure), DML (data manipulation), DQL (querying), DCL (permissions), and TCL (transactions).
2. Explain Like I'm 5
Think of a building: DDL is the architect building rooms, DML is the tenant moving furniture in, DQL is a visitor looking around, DCL is the security guard giving keys, and TCL is the contract signing.
3. Animation & Visual Simulation
Interactive Engine TraceSQL Command Types Visual Simulator
-- DDL creates, DML writes, DQL reads, TCL commits:
BEGIN;
INSERT INTO accounts (id, balance) VALUES (1, 1000.00);
COMMIT;
SELECT * FROM accounts;Active InputStep 1: Input & Parsing Stage
Initiating SQL Command Types
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
A banking application uses all five command types in daily operation.
| Category | Acronym | Core Verbs |
|---|---|---|
| Data Definition | DDL | CREATE, ALTER, DROP, TRUNCATE |
| Data Manipulation | DML | INSERT, UPDATE, DELETE |
| Data Query | DQL | SELECT |
| Data Control | DCL | GRANT, REVOKE |
| Transaction Control | TCL | COMMIT, ROLLBACK, SAVEPOINT |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectCREATE TABLE accounts (id INT PRIMARY KEY, balance NUMERIC(10,2));
BEGIN; UPDATE accounts SET balance = balance - 50 WHERE id = 1; UPDATE accounts SET balance = balance + 50 WHERE id = 2; COMMIT;
SELECT id, balance FROM accounts WHERE id IN (1, 2);
GRANT SELECT ON accounts TO auditor_role;
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| DDL Implicit Commits | In MySQL and Oracle, DDL statements cause an automatic commit of any open transaction (PostgreSQL supports transactional DDL). |
| DML Row Locks | DML operations acquire row-level exclusive locks until the transaction issues COMMIT or ROLLBACK. |
| DQL Concurrency | Under standard MVCC, SELECT statements do not lock out read or write operations. |
| DDL implicit commits in MySQL | DDL statements in MySQL implicitly commit the active transaction, whereas PostgreSQL supports transactional DDL. |
| AccessExclusiveLock acquired by DDL | DDL statements like ALTER TABLE or DROP TABLE acquire AccessExclusiveLock, blocking all concurrent SELECT, INSERT, UPDATE, and DELETE operations. |
7. Deep Dive: Transactional DDL in Modern Engines
PostgreSQL allows DDL statements (like CREATE TABLE or ALTER TABLE) to execute inside a BEGIN...COMMIT block, allowing atomic schema rollback if a migration step fails.
BEGIN; ALTER TABLE accounts ADD COLUMN currency VARCHAR(3) DEFAULT 'USD'; -- If something fails, ROLLBACK restores previous schema! COMMIT;
8. Advantages & Disadvantages
Advantages
- •Clear separation of duties between schema design, data mutation, and security.
- •Fine-grained access control through DCL grants.
- •Transactional safety via TCL guarantees all-or-nothing execution.
Disadvantages & Trade-offs
- •Running heavy DDL operations on production can cause table locks.
- •Uncommitted TCL blocks hold database locks and exhaust connection pools.
9. Common Interview Questions
Q1.What is the physical execution difference between DELETE and TRUNCATE?
DELETE is a DML operation that scans and marks individual row slots dead one by one, writing full transaction log records (WAL/Undo) for every deleted tuple and firing AFTER DELETE triggers. TRUNCATE is a DDL operation that deallocates entire physical disk pages from the storage engine, does not fire per-row triggers, resets identity sequences, and runs orders of magnitude faster.
| Metric | DELETE (DML) | TRUNCATE (DDL) |
|---|---|---|
| Operation Level | Row-by-row tuple deletion | Page-level disk block deallocation |
| WAL / Undo Volume | Heavy (logs every deleted row) | Minimal (logs page deallocation) |
| Triggers | Fires row-level triggers | Does NOT fire row-level triggers |
| Speed (10M rows) | Minutes (high I/O lock) | Milliseconds (instant metadata update) |
| WHERE Clause | Supported (selective deletion) | NOT supported (clears entire relation) |
Q2.Can you rollback a TRUNCATE statement inside a transaction in PostgreSQL vs MySQL?
In PostgreSQL, TRUNCATE is transactional and can be safely rolled back within a BEGIN...ROLLBACK block because PostgreSQL tracks page deallocation in WAL. In MySQL (InnoDB), TRUNCATE is non-transactional and implicitly commits any pending transaction, making rollback impossible.
Q3.What is DCL and how do role hierarchies and privilege cascades work?
Data Control Language (GRANT, REVOKE) manages database authorization. When privilege WITH GRANT OPTION is granted to a user, they can delegate privileges to other roles. If a parent role has their grant revoked with CASCADE, all child privileges granted down the chain are automatically revoked.
Q4.Why can executing an ALTER TABLE statement cause connection pool exhaustion?
ALTER TABLE requires an AccessExclusiveLock. Even if the ALTER itself is fast, if there is a long-running SELECT currently reading the table, the ALTER statement waits in the lock queue. All subsequent SELECTs behind the ALTER statement are blocked from reading the table, rapidly exhausting the application connection pool.
Q5.What is the difference between DML and DQL, and why are they sometimes grouped together?
DQL (Data Query Language) consists of SELECT and is strictly read-only, retrieving data without mutating state. DML (Data Manipulation Language) consists of INSERT, UPDATE, and DELETE which mutate data tuples and acquire row-level write locks. Some SQL standards group SELECT under DML because queries often form the basis of updates (e.g. INSERT INTO ... SELECT).
10. Interview-Ready Answer
“SQL is divided into five core sub-languages: DDL manages schemas, DML mutates rows, DQL retrieves data, DCL manages permissions, and TCL guarantees atomicity via COMMIT and ROLLBACK.”
11. Quick Test
Which of the following is a DDL (Data Definition Language) command?