Skip to main content
Beginnercomparison

What is the difference between DDL and DML commands in SQL? Where do COMMIT and ROLLBACK fit?

ReportedSource: GeeksforGeeks interview experience: Cognizant GenC 2024

Quick answer

DDL (CREATE/ALTER/DROP/TRUNCATE) defines structure and often auto-commits; DML (INSERT/UPDATE/DELETE) changes data inside transactions; COMMIT makes changes permanent, ROLLBACK undoes uncommitted ones.

DDL (Data Definition Language) defines structure: CREATE, ALTER, DROP, TRUNCATE, RENAME. In many databases (Oracle, MySQL) DDL auto-commits, so it cannot be rolled back; PostgreSQL supports transactional DDL.

DML (Data Manipulation Language) changes data: INSERT, UPDATE, DELETE, MERGE (and SELECT, sometimes classified as DQL). DML runs inside transactions.

TCL (Transaction Control Language) manages those transactions:

  • COMMIT makes the transaction's changes permanent and visible to others.
  • ROLLBACK undoes uncommitted changes.
  • SAVEPOINT marks a point to roll back to partially.
query.sql
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;   -- or ROLLBACK if either update failed

Also: DCL (GRANT, REVOKE) controls permissions.

Why COMMIT matters. Until commit, other sessions do not see the changes (depending on isolation level) and a crash discards them.

See the concept: Join fan-out

Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Discussion