Skip to content
Back to 50 Concepts
Topic 3 of 50foundationsBeginner

# 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).

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

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 Trace
Interactive Engine Simulation

SQL Command Types Visual Simulator

Step 1 of 3
SQL>-- DDL creates, DML writes, DQL reads, TCL commits: BEGIN; INSERT INTO accounts (id, balance) VALUES (1, 1000.00); COMMIT; SELECT * FROM accounts;Active Input

Step 1: Input & Parsing Stage

Engine Action: Validate SQL syntax and resolve schema metadata for SQL Command Types
SQL Input

Initiating SQL Command Types

Schema Catalog

Checking Table Invariants

How the Database Executes This:

The database engine inspects active tables, verifies permissions, and prepares the physical operation.

💡 Building construction: DDL is pouring concrete, DML is decorating rooms, DQL is taking a photo, DCL is handing out keys, and TCL is signing the deed.

4. Real-World Example

A banking application uses all five command types in daily operation.

SQL Command Categories
CategoryAcronymCore Verbs
Data DefinitionDDLCREATE, ALTER, DROP, TRUNCATE
Data ManipulationDMLINSERT, UPDATE, DELETE
Data QueryDQLSELECT
Data ControlDCLGRANT, REVOKE
Transaction ControlTCLCOMMIT, ROLLBACK, SAVEPOINT
Relational walkthrough: An administrator creates the table (DDL), grants access (DCL), customers transfer funds (TCL + DML), and auditors review logs (DQL).

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
DDL: Define Structure
CREATE TABLE accounts (id INT PRIMARY KEY, balance NUMERIC(10,2));
TCL & DML: Safe Transactional Write
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;
DQL: Query State
SELECT id, balance FROM accounts WHERE id IN (1, 2);
DCL: Control Permissions
GRANT SELECT ON accounts TO auditor_role;

6. Important Rules & Behaviors

RuleBehavioral Explanation
DDL Implicit CommitsIn MySQL and Oracle, DDL statements cause an automatic commit of any open transaction (PostgreSQL supports transactional DDL).
DML Row LocksDML operations acquire row-level exclusive locks until the transaction issues COMMIT or ROLLBACK.
DQL ConcurrencyUnder standard MVCC, SELECT statements do not lock out read or write operations.
DDL implicit commits in MySQLDDL statements in MySQL implicitly commit the active transaction, whereas PostgreSQL supports transactional DDL.
AccessExclusiveLock acquired by DDLDDL 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;
Caution: In MySQL, DDL statements implicitly commit any active transaction, making automated migration rollback impossible.

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.

MetricDELETE (DML)TRUNCATE (DDL)
Operation LevelRow-by-row tuple deletionPage-level disk block deallocation
WAL / Undo VolumeHeavy (logs every deleted row)Minimal (logs page deallocation)
TriggersFires row-level triggersDoes NOT fire row-level triggers
Speed (10M rows)Minutes (high I/O lock)Milliseconds (instant metadata update)
WHERE ClauseSupported (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

Scenario Question:

Which of the following is a DDL (Data Definition Language) command?

Select your answer: