← Back to SQL, page 1

sql-0010 · Easy fresher

What are DDL, DML, DQL, DCL and TCL in SQL? Give an example statement for each.

They are groups of SQL statements by purpose. DDL defines structure (CREATE, ALTER, DROP), DML changes data (INSERT, UPDATE, DELETE), DQL reads data (SELECT), DCL controls access (GRANT, REVOKE) and TCL manages transactions (BEGIN, COMMIT, ROLLBACK, SAVEPOINT).

Full answer

The labels are a way to talk about what a statement does.

  • DDL, data definition language, changes the shape of the database: CREATE TABLE, ALTER TABLE, DROP INDEX, TRUNCATE in many engines.
  • DML, data manipulation language, changes rows: INSERT, UPDATE, DELETE, MERGE.
  • DQL, data query language, reads rows: SELECT. Some books fold SELECT into DML.
  • DCL, data control language, manages permissions: GRANT SELECT ON orders TO analyst, REVOKE.
  • TCL, transaction control language, groups work into transactions: BEGIN, COMMIT, ROLLBACK, SAVEPOINT.

A short script that touches four of the five groups:

sql
CREATE TABLE canteen_bill (id INTEGER PRIMARY KEY, item TEXT, price INTEGER);
BEGIN;
INSERT INTO canteen_bill (item, price) VALUES ('Tea', 15), ('Samosa', 20);
COMMIT;
SELECT item, price FROM canteen_bill ORDER BY id;
itemprice
Tea15
Samosa20

The grouping has practical meaning. DDL often takes stronger locks than DML because it changes the structure every query depends on. Whether DDL can be rolled back depends on the engine. PostgreSQL and SQLite run DDL inside transactions, so a CREATE TABLE inside a rolled-back transaction disappears:

sql
BEGIN;
CREATE TABLE scratch (x INTEGER);
ROLLBACK;
SELECT COUNT(*) AS tables_named_scratch FROM sqlite_master WHERE name = 'scratch';
tables_named_scratch
0

MySQL commits implicitly before most DDL statements, so the same script would leave the table in place there (from the MySQL documentation, not run here). SQLite has no users, so it has no GRANT or REVOKE.

sql
CREATE TABLE canteen_bill (id INTEGER PRIMARY KEY, item TEXT, price INTEGER);
BEGIN;
INSERT INTO canteen_bill (item, price) VALUES ('Tea', 15), ('Samosa', 20);
COMMIT;
SELECT item, price FROM canteen_bill ORDER BY id;

BEGIN;
CREATE TABLE scratch (x INTEGER);
ROLLBACK;
SELECT COUNT(*) AS tables_named_scratch FROM sqlite_master WHERE name = 'scratch';

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • DDL: structure; DML: rows; DQL: reading; DCL: permissions; TCL: transactions
  • DDL usually takes heavier locks than DML
  • PostgreSQL and SQLite can roll back DDL; MySQL implicitly commits most DDL

Common mistakes

  • Assuming every engine can roll back a DROP TABLE inside a transaction
  • Listing the acronyms without saying why the distinction matters

Follow-ups

  • Is TRUNCATE DDL or DML, and why does it matter?
  • Why are schema migrations safer on an engine with transactional DDL?

Tips

  • Add one practical point, such as transactional DDL, after the definitions